2009-12-29 18 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