2015-04-28 80 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