.gitignore排除文件夹,但包括特定的子文件夹
我有一个文件夹 application/
,我将其添加到 .gitignore
。
在应用程序/
-folder中,有一个文件夹 application/language/gr
。
如何包含此文件夹?
我已经尝试过:
application/
!application/language/gr/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果排除
应用程序/
,则将始终排除其下的所有内容(即使以后的负面排除模式(“ Unignore”)可能与application/
下的某些内容相匹配。要做自己想做的事,您必须“ Unignore”要“ Unignore”的任何内容的每个父目录。通常,您最终会成对为这种情况编写规则:忽略目录中的所有内容,但不是某些子目录。
注意
尾随
/*
很重要:dir/
不包括名为dir
和(隐式)所有内容的目录。使用
dir/
,git永远不会在dir
下查看任何内容,因此永远不会将任何“非隔离”模式应用于dir 。
dir/*
对dir
本身没有说明;它只是不包括dir
下的所有内容。使用
dir/*
,git将处理dir
的直接内容,使其他模式有机会“取消”内容(!dir) /sub/
)。If you exclude
application/
, then everything under it will always be excluded (even if some later negative exclusion pattern (“unignore”) might match something underapplication/
).To do what you want, you have to “unignore” every parent directory of anything that you want to “unignore”. Usually you end up writing rules for this situation in pairs: ignore everything in a directory, but not some certain subdirectory.
Note
The trailing
/*
is significant:dir/
excludes a directory nameddir
and (implicitly) everything under it.With
dir/
, Git will never look at anything underdir
, and thus will never apply any of the “un-exclude” patterns to anything underdir
.dir/*
says nothing aboutdir
itself; it just excludes everything underdir
.With
dir/*
, Git will process the direct contents ofdir
, giving other patterns a chance to “un-exclude” some bit of the content (!dir/sub/
).Karsten Blees (kblees) for Git 1.9/2.0 (Q1 2014) clarifies that case:
在您的情况下:
您必须首先白列表文件夹,才能在给定文件夹中使用白列表文件。
更新2016年2月/2016年3月:
请注意,使用GIT 2.9.x/2.10(2016年中?),如果该文件的父目录被排除在外,则可以重新包含文件。
nguyễntháingọcduy(
pclouds
)因此,对于Git 2.9+,这实际上可能有效,但最终被恢复了:
Commit 59856de from Karsten Blees (kblees) for Git 1.9/2.0 (Q1 2014) clarifies that case:
gitignore.txt
: clarify recursive nature of excluded directoriesIn your case:
You must white-list folders first, before being able to white-list files within a given folder.
Update Feb/March 2016:
Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.
Nguyễn Thái Ngọc Duy (
pclouds
) is trying to add this feature:So with git 2.9+, this could have actually worked, but was ultimately reverted:
@chris Johnsen的答案很棒,但是有了较新的Git版本(1.8.2或更高版本),有一个双重星号模式,您可以利用一些更多的速记解决方案:
这样您就不必“ Unignore”父母。您要跟踪的子文件夹的目录。
使用GIT 2.17.0(不确定此版本之前的时间。例如:
@Chris Johnsen's answer is great, but with a newer versions of Git (1.8.2 or later), there is a double asterisk pattern you can leverage for a bit more shorthand solution:
This way you don't have to "unignore" parent directory of the subfolder you want to track.
With Git 2.17.0 (Not sure how early before this version. Possibly back to 1.8.2), using the
**
pattern combined with excludes for each subdirectory leading up to your file(s) works. For example:我发现此实际上有效。
I've found only this actually works.
关于此问题有很多类似的问题,所以我会发布我之前写的内容:
让我在机器上工作的唯一方法就是这样做:
注意您必须如何明确允许每个级别的内容允许您想包括。因此,如果我在主题深处有5个子目录,那么我仍然需要阐明这一点。
这是从 @yarin的评论中的: https://stackoverflow.com/a/5250314/16966153
这些是有用的主题:
?
**/wp-content/themes/**
或
/wp-content/themes/**/*
都不适合我。有很多反复试验!
There are a bunch of similar questions about this, so I'll post what I wrote before:
The only way I got this to work on my machine was to do it this way:
Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.
This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153
These were useful topics:
I also tried
and
**/wp-content/themes/**
or
/wp-content/themes/**/*
None of that worked for me, either. Lots of trial and error!
最简单,最佳方法是尝试手动添加文件(通常,这优先于
.gitignore
style规则):如果文件已经已经存在,则可能需要
-f
被忽略。您甚至可能需要-n
意图添加 flag,以建议您 Will 添加它们,但不要立即。我经常为新文件做这件事,我还没有准备好上演。这是一个a 答案的副本张贴在很容易成为重复的QA上。我在这里将其重新发布以提高可见性 - 我发现不拥有一系列Gitignore规则会更容易。
The simplest and probably best way is to try adding the files manually (generally this takes precedence over
.gitignore
-style rules):You might need
-f
if the file is already ignored. You may even want the-N
intent to add flag, to suggest you will add them, but not immediately. I often do this for new files I’m not ready to stage yet.This a copy of an answer posted on what could easily be a duplicate QA. I am reposting it here for increased visibility—I find it easier not to have a mess of gitignore rules.
我的Jetbrains Intellij Idea
.gitignore
-configuration,我需要排除整个.idea
文件夹。 >
“ noreferrer”> httpps:// https://github.com/github.com/daggerok/daggerignore-gitignore-igitignore-idea-idea-runea-runconconfigurations #排除-IDEA-EXCEPT-IDEARUNCONFIGURATIONS
My JetBrains IntelliJ IDEA
.gitignore
-configuration, where I need to exclude whole.idea
folder except.idea/runConfigurations
:Further details:
https://github.com/daggerok/gitignore-idea-runConfigurations#exclude-idea-except-idearunconfigurations
我想跟踪
/etc/nagios/
中的Nagios配置文件以及/usr/lib64/lib64/nagios/plugins/plugins/
中的插件。为此,我在/
中初始化了git repo,并使用了以下排除列表:git沿着这样的列表走下去:
I wanted to track Nagios configuration files located in
/etc/nagios/
together with the plugins in/usr/lib64/nagios/plugins/
. For this I have initialized a git repo in/
and used the following exclusion list:Git walks down the list like that:
填写此工作!
将一个名为
.gitignore
的文件添加到子文件夹中,然后为我add a file named
.gitignore
to subfolder, then fill withthis works for me!
我在这里发现了类似的情况,默认情况下,在Laravel中,
.gitignore
使用Asterix忽略所有内容,然后覆盖公共目录。(这也是与主要答案@chris Johnsen的解决方案相同的解决方案,也许有点薄,更简洁。)
如果您遇到OP方案,这是不够的。
如果要提交
public
的特定子文件夹,请在public> public/production
目录中说明您要包含的文件,这些文件是一个subfolter deep,例如,例如public/product/a/b.jpg
即使您特别添加了!/public/production
,!public/products/*<,它们也无法正确检测到它们。 /code>等。
解决方案是要确保您为这样的每个路径级别添加一个条目以覆盖所有路径。
I have found a similar case here, where in laravel by default,
.gitignore
ignores all using asterix, then overrides the public directory.( This is also the same solution as the main answer @Chris Johnsen, just a bit thinner and more concise maybe.)
This is not sufficient if you run into the OP scenario.
If you want to commit a specific subfolders of
public
, say for e.g. in yourpublic/products
directory you want to include files that are one subfolder deep e.g. to includepublic/products/a/b.jpg
they wont be detected correctly, even if you add them specifically like this!/public/products
,!public/products/*
, etc..The solution is to make sure you add an entry for every path level like this to override them all.
Gitignore-指定有意忽略的未跟踪文件。
除了特定目录 foo/bar (请注意/* - 没有斜线,通配符还将排除 foo/bar ):
WordPress 的另一个示例:
此处的更多信息: https:// git-scm.com/docs/gitignore
gitignore - Specifies intentionally untracked files to ignore.
Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):
Another example for WordPress:
More informations in here: https://git-scm.com/docs/gitignore
因此,由于许多程序员使用节点。符合此问题的用例是排除
node_modules
除一个模块模块-A
例如:例如:So , since many programmers uses node . the use case which meets this question is to exclude
node_modules
except one modulemodule-a
for example:添加其他答案:
这里是结果:
Add an additional answer:
here is result:
这对我有用:
This worked for me:
类似于
git add -f。
Similar to this comment, none of the solutions and patterns worked for me; forcing git to add the files and folders that should be excluded, worked:
git add -f .
特别是对于较旧的Git版本,大多数建议无法正常工作。
如果是这样,我将在目录中放置一个单独的.gitignore,无论其他设置如何,我希望将内容包含在其中,并允许所需的内容。
例如:
/.gitignore
/dependendency_files/.gitignore
,因此包括 /distionency_files(甚至.dll文件)中的所有内容都可以很好地包含。
Especially for the older Git versions, most of the suggestions won't work that well.
If that's the case, I'd put a separate .gitignore in the directory where I want the content to be included regardless of other settings and allow there what is needed.
For example:
/.gitignore
/dependency_files/.gitignore
So everything in /dependency_files (even .dll files) are included just fine.
在WordPress中,这对我有所帮助:
In WordPress, this helped me:
只是沿着目录结构走的另一个例子,以便准确地获得您想要的东西。注意:我没有排除
library/
,但library/**/*
&gt; git添加库
&gt; git状态
Just another example of walking down the directory structure to get exactly what you want. Note: I didn't exclude
Library/
butLibrary/**/*
> git add Library
> git status
我想跟踪jQuery Production JS文件,这有效:
I wanted to track jquery production js files and this worked:
我经常在CLI中使用此解决方法,其中我没有配置我的
.gitignore
,而是创建一个单独的.include
文件,我在其中定义(sub)我希望直接包含目录,或者直接或递归地忽略了.gitignore
。因此,我
在登台期间还使用之前使用。
对于OP,我建议使用
.include
具有以下行:注意:使用
cat
不允许使用别名(在中。Include
。 )用于指定$ home(或任何其他特定目录)。这是因为行homedir/app1/*
当传递到
时,使用上述命令将
添加为git添加'homedir/app1/*'
,并将字符封闭在单个引号('')中引号中的每个字符,从而阻止别名(例如 homedir )功能(请参阅 bash单引号)。这是我在repo .include 文件的示例。 a>。
I often use this workaround in CLI where instead of configuring my
.gitignore
, I create a separate.include
file where I define the (sub)directories I want included in spite of directories directly or recursively ignored by.gitignore
.Thus, I additionally use
during staging, before committing.
To the OP, I suggest using a
.include
which has these lines:NOTE: Using
cat
does not allow usage of aliases (within.include
) for specifying $HOME (or any other specific directory). This is because the linehomedir/app1/*
when passed to
git add
using the above command appears asgit add 'homedir/app1/*'
, and enclosing characters in single quotes ('') preserves the literal value of each character within the quotes, thus preventing aliases (such as homedir) from functioning (see Bash Single Quotes).Here is an example of a
.include
file I use in my repo here.