Git不会取消忽略目录

时间:2009-12-29 04:24:03

标签: git

我只想在一个更大的项目中跟踪git中的某些某些目录,其中大多数目录将被排除,只有少数目录会被跟踪。所以,我想在我的.gitignore中使用not-operators或者排除文件,ja?

为什么会这样:

% 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)

“not”语法适用于最高目录中的文件。

% 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
#

我怎样才能让这个给我作为git跟踪文件的baz和foo? (基本上,目录NOT-style globbing是如何工作的?!似乎没有。)

谢谢!

5 个答案:

答案 0 :(得分:18)

恢复旧帖子,万一其他人偶然发现:

如果要忽略当前目录中的所有文件夹/文件,请使用前导斜杠(/*/表示目录或/*表示文件);否则Git将以递归方式忽略该模式,这将导致未签名目录中的文件也被忽略......

考虑到这一点,!directory/模式可以很好地保存目录。

答案 1 :(得分:14)

要强制添加被忽略的文件夹,请使用:

git add -f my-ignored-folder

答案 2 :(得分:2)

不要提及.gitignore中的目录,只提及文件。这样做并明确添加您想要跟踪的目录应该可以满足您的需求:

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

这将让git跟踪bar和iwanthesefiles目录中的所有内容。

答案 3 :(得分: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")

答案 4 :(得分:0)

如果上述方法均无效,请尝试在.gitignore内部尝试此变体

Not working for me:
!build/**/*
Working for me:
!build**/*

由于某种原因,这是它对我有用的唯一方法。