git 如何向 .gitignore 添加一些东西,以便匹配不是递归的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2476718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:10:35  来源:igfitidea点击:

How to I add something to the .gitignore so that the match is not recursive?

gitignoregitignore

提问by pauldoo

How to I add something to the .gitignoreso that the match is not recursive?

如何向 中添加一些内容以.gitignore使匹配不是递归的?

For example, I wish to ignore the directory fooand the file bar.txtin the current directory, but not any that exist in subdirectories.

例如,我希望忽略当前目录中的目录foo和文件bar.txt,但不要忽略子目录中存在的任何目录和文件。

I have tried this for my .gitignorefile:

我已经为我的.gitignore文件尝试过这个:

foo/
bar.txt

But unfortunately git applies this recursively, so that otherdir/bar.txtand otherdir/foo/also get ignored, which is not what I want.

但不幸的是git的递归地应用这个,所以otherdir/bar.txtotherdir/foo/也被忽略,这不是我想要的。

(Is there a command in git that shows me all ignored files, and reference the .gitignorefile that is responsible for the file being ignored? This would be useful for debugging.)

(git 中是否有一个命令可以显示所有被忽略的文件,并引用.gitignore导致文件被忽略的文件?这对调试很有用。)

回答by pauldoo

The solution is to place a leading slash on the .gitignoreentries:

解决方案是在.gitignore条目上放置一个前导斜杠:

/foo/
/bar.txt

(I thought I tried this before posting on StackOverflow, but clearly I hadn't tried it properly, as this works perfectly.)

(我以为我在 StackOverflow 上发布之前尝试过这个,但显然我没有正确尝试,因为它完美地工作。)

回答by hermannloose

From the gitignore manpage:

从 gitignore 联机帮助页:

An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.

一个可选的前缀!否定模式;任何被先前模式排除的匹配文件将再次包含在内。如果否定模式匹配,这将覆盖较低优先级的模式源。

So !*as the first line in your .gitignore will clear all previous patterns.

因此!*,您的 .gitignore 中的第一行将清除所有以前的模式。