Why is my new .gitignore automatically ignored?

。_饼干妹妹 提交于 2019-12-25 01:45:08

问题


So, after converting my repository to git and doing the first build, some build directories showed up in 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/

So, of course I need a .gitignore, and since I didn't want to type these directory-names again, I used this command:

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

Since git status -s showed this

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

before, I assumed the new .gitignore file to contain these lines:

classes/
doku/
texdoku/
tmp/

But then:

$ 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")

Yes, it ignored the four directories, but also the new .gitignore file. Why?

$ 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

Huh. Why is my .gitignore ignored? I remember that in the last project I could add it to the repository. I searched quite a time, and googled what else could cause this file to be ignored - .git/info/excludes has only commented lines, and all parent-directories up to / have no .gitignore. Also git config core.excludesfile shows nothing.

Why?


回答1:


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

The > .gitignore redirection at the end of the pipeline created the .gitignore file before git status did its directory listing. So, in fact the result was

.gitignore
classes/
doku/
texdoku/
tmp/

which got written in the .gitignore file. Thus, the .gitignore ignored itself. Editing the file to remove the first line solved the problem, of course.



来源:https://stackoverflow.com/questions/5399628/why-is-my-new-gitignore-automatically-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!