.gitignore 排除文件夹但包含特定子文件夹
我有一个文件夹 application/
,我将其添加到 .gitignore
中。
在 application/
文件夹内有一个文件夹 application/language/gr
。
我怎样才能包含这个文件夹?
我试过这个:
application/
!application/language/gr/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果排除
application/
,则其下的所有内容都将始终被排除(即使稍后的某些否定排除模式(“unignore”)可能与application/
下的某些内容匹配)。要执行您想要的操作,您必须“取消忽略”您想要“取消忽略”的任何内容的每个父目录。通常,您最终会成对地为这种情况编写规则:忽略目录中的所有内容,但不忽略某些特定的子目录。
注意
尾随的
/*
很重要: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/
).从 ="noreferrer">Karsten Blees (kblees) for Git 1.9/2.0 (Q1 2014) 澄清了这种情况:
gitignore.txt
:阐明排除目录的递归性质在您的情况下:
您必须首先将文件夹列入白名单,然后才能将给定文件夹中的文件列入白名单。
2016 年 2 月/3 月更新:
请注意,使用 git 2.9.x/2.10(2016 年中?),如果排除该文件的父目录,则可以重新包含该文件 如果重新包含的路径中没有通配符。
Nguyễn Thái Ngọc Duy (
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 或更高版本),您可以利用双星号模式来获得更多速记解决方案:
这样您就不必“忽略”父级您要跟踪的子文件夹的目录。
使用 Git 2.17.0(不确定此版本之前有多早。可能回到 1.8.2),使用
**
模式结合排除导致文件的每个子目录可以工作。例如:@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/1696153
这些都是有用的主题:
我也尝试
过
**/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
样式规则):如果文件已经存在,您可能需要
-f
被忽略。您甚至可能需要-N
意图添加标志,以建议您将添加它们,但不是立即添加。我经常对尚未准备好上演的新文件执行此操作。这是发布在很容易重复的质量检查上的答案副本。我将其重新发布到此处是为了提高可见性——我发现不使用混乱的 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
配置,我需要排除除.idea/runConfigurations
之外的整个.idea
文件夹:更多详细信息:
https://github.com/daggerok/gitignore-idea-runConfigurations #exclude-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/nagios/plugins/
中的插件。为此,我在/
中初始化了一个 git 存储库,并使用了以下排除列表: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/products
目录中,您希望包含一个子文件夹深的文件,例如包含public/products/a/b.jpg
即使您像这样专门添加它们!/public/products
,!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 中的所有内容strong>):
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.满足此问题的用例是排除除一个模块
module-a
之外的node_modules
,例如: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 版本,大多数建议都不会那么有效。
如果是这种情况,我会将一个单独的 .gitignore 放在我想要包含内容的目录中,而不管其他设置如何,并允许需要的内容。
例如:
/.gitignore
/dependency_files/.gitignore
因此 /dependency_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:
与此评论类似,所有解决方案和模式都不起作用为我;强制 git 添加应排除的文件和文件夹,有效:
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 .
这是沿着目录结构查找所需内容的另一个示例。注意:我没有排除
Library/
,而是排除Library/**/*
> git add 库
> 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 生产 js 文件,这有效:
I wanted to track jquery production js files and this worked:
我经常在 CLI 中使用此解决方法,而不是配置我的
.gitignore
,而是创建一个单独的.include
文件,在其中定义(子)尽管.gitignore
直接或递归地忽略了我想要包含的目录。因此,我
还在提交之前的暂存期间使用。
对于OP,我建议使用包含以下几行的
.include
:注意:使用
cat
不允许使用别名(在.include
内) )用于指定 $HOME (或任何其他特定目录)。这是因为homedir/app1/*
行当使用上述命令传递给 git add 时,显示为 git add 'homedir/app1/*' ,并且将字符括在单引号 ('') 中保留了文字值引号内的每个字符,从而防止别名(例如 homedir)发挥作用(请参阅 Bash 单引号)。
以下是我在存储库中使用的
.include
文件的示例此处。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.