Git似乎忽略了我的.gitignore文件-.gitignorefile是否已损坏?Git希望使用哪种文件格式、区域设置或文化?

我的.gitignore:

# This is a comment
debug.log
nbproject/

git状态的输出:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       debug.log
#       nbproject/
nothing added to commit but untracked files present (use "git add" to track)

我希望debug.log和nbproject/不出现在未跟踪的文件列表中。

我应该从哪里开始解决这个问题?


当前回答

我也有同样的问题。我认为问题是CR与CR+LF之间的差异。我使用CMD(在Windows 7上)和以下命令将这些东西存储在.gitignore中:

Bad:

echo "file_to_be_ignored.py" >> .gitignore<br>
echo "*~" >> .gitignore

Etc.

问题是,该命令没有为Git放置正确的行尾标记,以识别换行符(当Git需要另一行时,可以是CR或CR+LF)。我通过手动替换Vim中的每一个换行符来解决了这个问题(Vim来拯救!),它工作得很好。

尝试在Notepad++或Vim中编辑.gitignore(理想情况下)。即使文件看起来格式正确,也尝试替换换行符。我知道,这听起来很奇怪,但它对我有用

其他回答

我注意到.gitignore的编码产生了影响——如果文件是Unicode,它会被忽略,如果是ASCII,它不会被忽略。

流程:

验证状态:PS>git状态创建一个函数以获取文件编码Test.gitignore的编码:PS>Get-FileEncoding.gitignor将编码更改为ASCII:PS>Set-Content.gitignore-encoding ASCII-Value(Get-Content.gitignore)确认:PS>git状态

这里其他答案没有涉及的一个棘手问题是,如果您有内联注释,.gitignore文件将无法工作,如下所示:

foo/bar # The bar file contains sensitive data so we don't want to make this public

因此,如果您确实有这样的评论,请这样更改它们:

# The bar file contains sensitive data so we don't want to make this public
foo/bar

如果您是Notepad++用户,请尝试执行以下操作:

使用Notepad++打开.gitignore文件,然后执行以下操作:

菜单编辑→ EOL转换→ Windows格式→ 拯救

再次尝试使用gitstatus,看看它是否适合您。

我在这里发布了类似问题的答案。

我的问题是(正如OP所建议的)一个损坏的.gitignore文件。我不相信这是真的,并忽略了这种可能性,直到其他一切都失败了。vi中没有显示损坏,但文件开头有两个字节导致.gitignore文件被忽略。对我来说,这些只有在我键入cat.gitignore时才会出现,它显示:

��# Built application files
*.apk
*.ap_

# ...

我不知道这些结果是如何出现的,但重新创建文件解决了问题。损坏文件的十六进制分析显示如下:

user@dev ~/project/myproject $ xxd -b .gitignore
00000000: 11111111 11111110 00100011 00000000 00100000 00000000  ..#. .
00000006: 01000010 00000000 01110101 00000000 01101001 00000000  B.u.i.

此问题的另一个原因是语句前的空格或制表符:

例子:

# Be aware of the following:
 notWorkingIgnore.*
workingIgnore.*

正如下面的评论所指出的,尾随空格也是一个问题:

# Be aware of the following:
notWorkingIgnore.* #<-Space
workingIgnore.*#<-Nospace