2011-03-23 55 views
0

所以,我轉換到存儲庫Git和做第一個構建之後,一些編譯目錄中git status出現了:爲什麼我的新.gitignore會自動忽略?

# 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只有註釋行,並且所有父目錄達到/沒有.gitignore。另外git config core.excludesfile什麼也沒有顯示。

爲什麼?

回答

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

在管道末端的> .gitignore重定向創建.gitignore文件以前git status做它的目錄列表。所以,其實結果是

.gitignore 
classes/ 
doku/ 
texdoku/ 
tmp/ 

寫在.gitignore文件中。因此,.gitignore忽略了自己。當然,編輯文件以刪除第一行解決了這個問題。

+0

你超出了你自己!不過,我喜歡自動創建.gitignore,並且使用相對較新的'git status -s',非常好。 (順便說一句,'--porcelain'目前與'-s, - short'相同,但保證不會改變它的輸出格式。) – Cascabel 2011-03-23 04:36:15