git 是否可以忽略子目录中的 .gitignore 规则?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3719574/
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
is it possible to ignore .gitignore rules in subdirectory?
提问by jeon
I want to ignore all of .gitignore rules in subdirectory except .gitignore rules in root.
我想忽略子目录中的所有 .gitignore 规则,除了 root 中的 .gitignore 规则。
for example)
I have already .gitignore
file in a directory structure /a
.
例如)我已经.gitignore
在目录结构中创建了文件/a
。
And also have .gitignore
file in /a/b
. Assume /a
have a.txt
b.txt
files.
并且还有.gitignore
文件在/a/b
. 假设/a
有a.txt
b.txt
文件。
/a/b
has .config
file. .gitignore
defines .config
in /a/b
.
/a/b
有.config
文件。.gitignore
定义.config
在/a/b
。
.config
file will be ignored by .gitignore
in /a/b
.
.config
文件将被.gitignore
in忽略/a/b
。
But I really want to track .config
file by ignoring rules of .gitignore
in subdirectory.
但我真的想.config
通过忽略.gitignore
子目录中的规则来跟踪文件。
Is it possible?
是否可以?
Thanks in advance
提前致谢
回答by signine
Your question isn't too clear, but if there are some subdirectories where you want different gitignore rules, you can just create a new .gitignore in that directory.
您的问题不太清楚,但是如果您需要在某些子目录中使用不同的 gitignore 规则,则只需在该目录中创建一个新的 .gitignore 即可。
Say you have:
说你有:
project/.gitignore project/stuff/.gitignore
The .gitignore in stuff/ will override the .gitignore in the root directory.
stuff/ 中的 .gitignore 将覆盖根目录中的 .gitignore 。
git help gitignore
or man gitignore
has quite a bit more information about what's possible.
git help gitignore
或者man gitignore
有更多关于可能的信息。
回答by Nick Zalutskiy
Lets say you want to include node_modules
in your repository, but some module down the line has .gitignore
of its own. You can negate the rules by adding the following to your root .gitignore
file:
假设您想包含node_modules
在您的存储库中,但该行的某些模块有.gitignore
自己的。您可以通过将以下内容添加到根.gitignore
文件来否定规则:
# Make sure to include everything in node_modules.
!node_modules/**
回答by Ross Patterson
The key to the problem here is that a parent directory is excluded. From $ man gitignore
:
这里问题的关键是排除了父目录。来自$ man gitignore
:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
如果排除了该文件的父目录,则无法重新包含该文件。出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都不起作用,无论它们在哪里定义。
Here's how I've gotten it to work. Given:
这就是我让它工作的方式。鉴于:
/a/...
/b/foo
/c/...
/.gitignore
:
/.gitignore
:
/*
!/b
Then /b/foo
will show up as included files while everything in /a
and /c
will be excluded. The trade off is that you have to do add a negation for all of the parents of any directory in which you want to include files.
然后/b/foo
将显示为包含的文件,而在一切/a
和/c
将被排除在外。权衡是您必须为要在其中包含文件的任何目录的所有父目录添加否定。
回答by helloiloveit
As far as i know there's no way to do it. As the local .gitignore will overwrite the root ginignore My workaround is adding manually.
据我所知,没有办法做到这一点。由于本地 .gitignore 将覆盖根 ginignore 我的解决方法是手动添加。
git add --force [ your file and folder]