git .gitignore 中的异常

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

Exceptions in .gitignore

gitgitignore

提问by pistacchio

How can I add an exception to .gitignore, like "ignore all the .dll files BUTmyfile.dll"?

如何向 .gitignore 添加异常,例如“忽略所有 .dll 文件myfile.dll”?

回答by Ben James

Use !to negate the pattern:

使用!否定的模式:

*.dll
!myfile.dll

回答by cubuspl42

If you want to ignore whole folder, except some specific files, then write:

如果你想忽略整个文件夹,除了一些特定的文件,然后写:

MyFolder/*
!MyFolder/CoolFile.txt

This won'twork:

将不工作:

MyFolder/
!MyFolder/CoolFile.txt

回答by Anver Sadhat

You can also ignore folderslike

您也可以忽略文件夹,

!src/main/resources/archetype-resources/**/*

you can also ignore nested folder with patternslike

你也可以忽略带有图案的嵌套文件夹一样

!**/src/test/resources/**/*

回答by kamayd

You can have several .gitignorefiles working together in a hierarchical manner to achieve your goal. At the root level you may have:

您可以将多个.gitignore文件以分层方式协同工作以实现您的目标。在根级别,您可能拥有:

root

*.dll

inside the folder having the myfile.dllyou can add another .gitignorefile like so:

在文件夹中,myfile.dll您可以添加另一个.gitignore文件,如下所示:

root/lib/folderwithMyFiledll

root/lib/folderwithMyFiledll

!myfile.dll

more info here

更多信息在这里

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt". It is possible to re-include a file if a parent directory of that file is excluded if certain conditions are met. See section NOTES for detail.

一个可选的前缀“!” 这否定了模式;任何被先前模式排除的匹配文件将再次包含在内。在第一个“!”前面放一个反斜杠(“\”)对于以文字“!”开头的模式,例如,“!important!.txt”。如果满足某些条件,则可以重新包含文件,如果该文件的父目录被排除在外。有关详细信息,请参阅注释部分。

回答by Robbiegod

I did this because I have a folder called /modulesthat I want to ignore, except everything in the /modules/customfolder. This worked for me. Now my custom modules will get synced to GitHub.

我这样做是因为我有一个名为/modules我想忽略的/modules/custom文件夹,文件夹中的所有内容除外。这对我有用。现在我的自定义模块将同步到 GitHub。

/modules/*
!/modules/custom/