为什么我的新.gitignore会自动被忽略?

时间:2011-03-23 00:44:42

标签: git bash redirect gitignore

因此,在将我的存储库转换为git并进行第一次构建之后,一些构建目录显示在git status中:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   build.xml
#       modified:   src/ant/common-tasks.xml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       classes/
#       doku/
#       texdoku/
#       tmp/

所以,当然我需要.gitignore,因为我不想再次输入这些目录名,我使用了这个命令:

git status -s | grep '?' | cut -b 4- > .gitignore

由于git status -s显示了这个

 M build.xml
 M src/ant/common-tasks.xml
?? classes/
?? doku/
?? texdoku/
?? tmp/

之前,我假设新的.gitignore文件包含这些行:

classes/
doku/
texdoku/
tmp/

但是:

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   build.xml
#       modified:   src/ant/common-tasks.xml
#
no changes added to commit (use "git add" and/or "git commit -a")

是的,它忽略了四个目录,但也忽略了新的.gitignore文件。为什么呢?

$ git add .gitignore
The following paths are ignored by one of your .gitignore files:
.gitignore
Use -f if you really want to add them.
fatal: no files added

咦。为什么我的.gitignore被忽略了?我记得在上一个项目中我可以将它添加到存储库中。我搜索了很长时间,并且搜索了其他可能导致此文件被忽略的内容 - .git/info/excludes只有注释行,而/之前的所有父目录都没有.gitignoregit config core.excludesfile也没有显示任何内容。

为什么?

1 个答案:

答案 0 :(得分:4)

git status -s | grep '?' | cut -b 4- > .gitignore

管道末尾的> .gitignore重定向在 .gitignore执行其目录列表之前创建了git status文件。所以,事实上结果是

.gitignore
classes/
doku/
texdoku/
tmp/

写在.gitignore文件中。因此,.gitignore忽略了自己。当然,编辑文件以删除第一行解决了问题。