git 否定模式如何在 .gitignore 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2820255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do negated patterns work in .gitignore?
提问by Chris Perkins
I am attempting to use a .gitignore file with negated patterns (lines starting with !), but it's not working the way I expect.
我正在尝试使用带有否定模式的 .gitignore 文件(以 ! 开头的行),但它没有按我预期的方式工作。
As a minimal example, I have the folllowing directory structure:
作为一个最小的例子,我有以下目录结构:
C:/gittest
-- .gitignore
-- aaa/
-- bbb/
-- file.txt
-- ccc/
-- otherfile.txt
and in my gitignore file, I have this:
在我的 gitignore 文件中,我有这个:
aaa/
!aaa/ccc/
My understanding (based on this doc page) is that the file aaa/ccc/otherfile.txt should not be ignored, but in fact git is ignoring everything under aaa.
我的理解(基于此文档页面)是不应忽略文件 aaa/ccc/otherfile.txt,但实际上 git 忽略了 aaa 下的所有内容。
Am I misunderstanding this sentence: "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."?
我是否误解了这句话:“一个可选的前缀!它否定了模式;任何被先前模式排除的匹配文件都将再次包含在内。”?
BTW, this is on Windows with msysgit 1.7.0.2.
顺便说一句,这是在带有 msysgit 1.7.0.2 的 Windows 上。
回答by Cascabel
I think that what you actually want to do is:
我认为你真正想做的是:
aaa/*
!aaa/ccc
You're telling it "don't look in aaa
" so it never even examines the path aaa/ccc
. If you use the wildcard, it still reads the contents of aaa
, then each entry matches the wildcard and is ignored, except aaa/ccc
which gets put back in.
你告诉它“不要看aaa
”,所以它甚至从不检查路径aaa/ccc
。如果您使用通配符,它仍会读取 的内容aaa
,然后每个条目都与通配符匹配并被忽略,除非aaa/ccc
被放回。
回答by xmak
If you want to exclude everything in aaa
, but include aaa/ccc
and everything beneath it, you should use:
如果要排除 中的所有内容aaa
,但包含aaa/ccc
其下的所有内容,则应使用:
aaa/*
!aaa/ccc
!aaa/ccc/*
The first line tells git to ignore everthing beneath aaa
, the second tells it not to ignore the folder aaa/ccc
which actually "enables" the third line which then tells it not to ignore everything beneath aaa/ccc
.
第一行告诉 git 忽略下面的所有内容aaa
,第二行告诉它不要忽略aaa/ccc
实际上“启用”第三行的文件夹,然后告诉它不要忽略下面的所有内容aaa/ccc
。
回答by alexkb
If anyone's still not seeing newly un-ignored items in a git status
running a git update-index
before hand can help git to see the changes (at least in version 1.9.x of gitbash).
如果有人在git status
运行git update-index
之前仍然没有看到新的未被忽略的项目,则可以帮助 git 查看更改(至少在 gitbash 的 1.9.x 版中)。