2009-12-29 235 views
12

我想在一個大項目中只跟蹤某些 git中的目錄,其中大多數目錄將被排除,只有少數目錄會被跟蹤。所以,我想在我的.gitignore中使用非運算符,或者排除文件,ja?Git不會忽略目錄

這究竟是爲什麼:

% mkdir wtfgit 
% cd wtfgit 
% git init 
Initialized empty Git repository in /home/foobar/wtfgit/.git/ 
% mkdir iwantthesefiles 
% touch iwantthesefiles/foo 
% mkdir idontwantthesefiles 
% touch idontwantthesefiles/bar 
% vim .gitignore 
% cat .gitignore 
* 
!iwantthesefiles/   
% git add --all 
% git status 
# On branch master 
# 
# Initial commit 
# 
nothing to commit (create/copy files and use "git add" to track) 

的「不」語法能正常工作的最高目錄中的文件。

% touch baz   
% vim .gitignore 
% cat .gitignore 
* 
!iwantthesefiles/ 
!baz 
% git add --all 
% git status  
# On branch master 
# 
# Initial commit 
# 
# Changes to be committed: 
# (use "git rm --cached <file>..." to unstage) 
# 
# new file: baz 
# 

我該如何得到這個給我baz和foo作爲git跟蹤文件? (基本上,NOT-style globbing目錄是怎樣工作的?!好像不是這樣)!

謝謝!

回答

2

不要在你的.gitignore中提到目錄,只是文件。這樣做並明確將要跟蹤應該給你你想要的目錄:

 
$ cat > .gitignore << EOF 
* 
!bar 
EOF 
$ git add -f iwantthesefiles 

目錄iwanthesefiles這將讓git的推力杆和一切。

0

Git的行爲方式如下:它不會忽略存儲庫中的文件。另外,要排除目錄,以斜線結尾。正如你在下面的例子中看到的,只有iwantthesefiles/baz被跟蹤。

$ cat .gitignore 
*/ 
!iwantthesefiles 
$ git status 
# On branch master 
# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 
# 
#  .gitignore 
#  iwantthesefiles/baz 
nothing added to commit but untracked files present (use "git add" to track) 
$ ls -lhR 
.: 
total 8.0K 
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:24 idonntwantthesefiles 
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:24 iwantthesefiles 

./idonntwantthesefiles: 
total 0 
-rw-r--r-- 1 user group 0 2009-12-29 14:22 bar 
-rw-r--r-- 1 user group 0 2009-12-29 14:24 baz 

./iwantthesefiles: 
total 0 
-rw-r--r-- 1 user group 0 2009-12-29 14:24 baz 
-rw-r--r-- 1 user group 0 2009-12-29 14:19 foo 
$ rm -r * && git reset --hard && ls -lhR 
HEAD is now at 698c66a monkey 
.: 
total 4.0K 
drwxr-xr-x 2 user group 4.0K 2009-12-29 14:25 iwantthesefiles 

./iwantthesefiles: 
total 0 
-rw-r--r-- 1 user group 0 2009-12-29 14:25 foo 

而且,要證明一個跟蹤文件覆蓋的.gitignore:

$ mkdir idonntwantthesefiles 
$ touch idonntwantthesefiles/baz 
$ 
git add idonntwantthesefiles/baz 
$ git commit -m 'llama' 
Created commit a62b6d5: llama 
0 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 idonntwantthesefiles/baz 
$ echo foobar > idonntwantthesefiles/baz 
$ git status 
# On branch master 
# Changed but not updated: 
# (use "git add <file>..." to update what will be committed) 
# 
#  modified: idonntwantthesefiles/baz 
# 
# Untracked files: 
# (use "git add <file>..." to include in what will be committed) 
# 
#  .gitignore 
no changes added to commit (use "git add" and/or "git commit -a") 
17

回想起舊的文章,萬一別人絆倒在此:

如果你想忽略所有的文件夾/文件,使用前導斜槓(/*/用於目錄或/*用於文件);否則Git會遞歸地忽略這個模式,這會導致無匹配目錄中的文件也被忽略...

考慮到這一點,!directory/模式取消了對目錄的檢查。

+2

對於我來說,我不得不添加兩行:!'腳本/'和'腳本/ *' – user151841 2015-09-30 14:31:40

14

有力地補充被忽略的文件夾,使用:

git add -f my-ignored-folder