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

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


当前回答

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

例子:

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

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

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

其他回答

即使到目前为止你还没有跟踪这些文件,Git似乎也可以在你将它们添加到.gitignore之后“知道”它们。

警告:首先提交或隐藏当前更改,否则您将丢失它们。

然后从Git存储库的顶部文件夹运行以下命令:

git rm -r --cached .
git add .
git commit -m "fixed untracked files"

每当我遇到git正在跟踪.gitignore中列出的文件的情况时,我会使用以下方法:

git update-index --skip-worktree <file_name>

这里的所有答案实际上都是变通方法。在运行gitinit之前,需要创建.gitignore文件。否则git永远不会知道你需要忽略这些文件,因为它们已经被跟踪了。

echo .idea/ >> .gitignore
git init

如果您每天都在开发,我建议您将习惯性忽略的文件添加到~/.gitignore_global文件中。这样,git就可以知道您(意思是“您的用户”,因为它是您的主目录中的文件)通常忽略的文件。

我遇到了这个问题,一个.gitignore文件包含以下行:

lib/ext/

我刚刚意识到,事实上,这个目录是指向其他文件夹的符号链接:

ls -la lib/ext/
lrwxr-xr-x 1 roipoussiere users 47 Feb  6 14:16 lib/ext -> /home/roipoussiere/real/path/to/the/lib

在lib/ext/行上,Git实际上查找一个文件夹,但符号链接是一个文件,因此我的lib文件夹不会被忽略。

我通过用.gitignore文件中的lib/ext替换lib/ext/来解决这个问题。

我刚刚遇到这个问题。gitignore文件中的内容继续出现在未跟踪文件列表中。

我用这个来创建忽略文件:

echo "node_modules" > .gitignore

事实证明,双引号导致了我的问题。我删除了忽略文件,然后再次使用了不带引号的命令,结果如预期。我不需要搞乱文件编码。我在使用Cmder的Windows 10机器上。

例子:

echo node_modules > .gitignore