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

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

is it possible to ignore .gitignore rules in subdirectory?

git

提问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 .gitignorefile in a directory structure /a.

例如)我已经.gitignore在目录结构中创建了文件/a

And also have .gitignorefile in /a/b. Assume /ahave a.txtb.txtfiles.

并且还有.gitignore文件在/a/b. 假设/aa.txtb.txt文件。

/a/bhas .configfile. .gitignoredefines .configin /a/b.

/a/b.config文件。.gitignore定义.config/a/b

.configfile will be ignored by .gitignorein /a/b.

.config文件将被.gitignorein忽略/a/b

But I really want to track .configfile by ignoring rules of .gitignorein 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 gitignoreor man gitignorehas quite a bit more information about what's possible.

git help gitignore或者man gitignore有更多关于可能的信息。

回答by Nick Zalutskiy

Lets say you want to include node_modulesin your repository, but some module down the line has .gitignoreof its own. You can negate the rules by adding the following to your root .gitignorefile:

假设您想包含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/foowill show up as included files while everything in /aand /cwill 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]