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/不出现在未跟踪的文件列表中。

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


当前回答

在不向项目添加另一个提交的情况下,一行代码就足以让.gitignore按预期工作:

git rm -r --cached debug.log nbproject

这将从存储库中删除它们,但仍保留它们。简单地说,它会删除与它们相关的任何更改历史记录,并且不会在将来的任何提交中跟踪它们的更改。你可以在这里找到更好的解释。

其他回答

我的没有工作,因为我确实创建了一个名为.gitignore的文本文档

相反,创建一个文本文档,在Notepad++中打开它,然后另存为.gitignore

保存时,确保从下拉列表中选择所有类型(*.*)。


或者在gitbash中,只需使用touch.gitignore

这里其他答案没有涉及的一个棘手问题是,如果您有内联注释,.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

在不向项目添加另一个提交的情况下,一行代码就足以让.gitignore按预期工作:

git rm -r --cached debug.log nbproject

这将从存储库中删除它们,但仍保留它们。简单地说,它会删除与它们相关的任何更改历史记录,并且不会在将来的任何提交中跟踪它们的更改。你可以在这里找到更好的解释。

您也可以使用sudo命令编辑.gitignore文件。我遇到了同样的问题,在执行命令gitstatus时,我仍然可以看到“应该被忽略”的文件。

在使用nano.gitignore而不是sudo nano.gitinore进行编辑时,我可以看到正确的反射。

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

例子:

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

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

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