2015-04-28 144 views
2

我試圖將一個未跟蹤的文件添加到.gitignore中的某個目錄中的回購站。.gitignore:跟蹤忽略目錄中的單個文件

我使用了以下內容:

parent/child/* 
parent/child/child-02/* 
!parent/child/child-02/file.txt 

這通常工作在第一胎目錄,但不是在上述格式。我錯過了什麼?

回答

5

一種方法是強制添加單個文件。一旦跟蹤,git將繼續跟蹤文件,而不管它是否被忽略或不在您的任何.gitignore中。

所以,簡單地使用下面將工作:

git add -f parent/child/child-02/file.txt 
git commit -m "added file.txt" 

如果你真的想糾正你的.gitignore文件,你將不得不增加一個規則(!parent/child/child-02)。這實際上告訴git不要忽略child-02目錄,所以後續規則工作:

parent/child/* 
!parent/child/child-02 
parent/child/child-02/* 
!parent/child/child-02/file.txt 

DEMO

test $ mkdir repo && cd repo && git init 
repo $ mkdir -p parent/child/child-02 
repo $ touch file parent/file parent/child/file parent/child/child-02/file.txt 
repo $ cat .gitignore 
parent/child/* 
!parent/child/child-02 
parent/child/child-02/* 
!parent/child/child-02/file.txt 
repo $ git add . 
repo $ git status 
     new file: .gitignore 
     new file: file 
     new file: parent/child/child-02/file.txt 
     new file: parent/file 
+0

'父/子/ * 父/子!/child-02 parent/child/child-02/* !parent/child/child-02/file.txt'是我需要的 - Thnx! – CelticParser

+0

我不得不提及,在'git status'中,你會看到整個目錄'parent'被標記爲未被跟蹤。這讓我感到困惑,所以我從[這個答案](http://stackoverflow.com/a/3801554/1713660)使用'git ls-files --other --exclude-standard'來確保我只添加一個文件 – vladkras