Git无法取消忽略一个目录

13
我想在一个大项目中仅跟踪特定目录,其中大部分目录将被排除,只有少数目录将被跟踪。因此,我想在我的.gitignore或排除文件中使用not运算符,对吗?
为什么会发生这种情况:
% 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?(基本上,目录中的非样式通配符匹配根本不起作用!)

谢谢!

5个回答

18

恢复一篇旧帖,以防其他人跌倒:

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

有了这个想法,!directory/ 模式可以很好地取消忽略目录。


2
对我来说,我需要添加两行代码:!scripts/!scripts/* - user151841

15

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

git add -f my-ignored-folder

2
不要在.gitignore中提及目录,只需提及文件。这样做并明确添加您想要跟踪的目录应该可以实现您想要的效果: $ cat > .gitignore << EOF * !bar EOF $ git add -f iwantthesefiles
这将使git跟踪bar和目录iwantthesefiles中的所有内容。

1
如果以上方法都不起作用,请尝试在 .gitignore 文件中使用以下变体。
Not working for me:
!build/**/*
Working for me:
!build**/*

由于某种原因,这是我唯一可行的方法。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接