is it possible to ignore .gitignore rules in subdirectory?
除了根目录中的.gitignore规则外,我想忽略子目录中的所有.gitignore规则。
例如)
我已经在目录结构/a中具有.gitignore文件。
并且在/a/b中也具有.gitignore文件。假设/a有a.txt b.txt个文件。
/a/b具有.config文件。 .gitignore在/a/b中定义.config。
.config文件将被/a/b中的.gitignore忽略。
但是我真的想通过忽略子目录中.gitignore的规则来跟踪.config文件。
有可能吗?
预先感谢
您的问题不太清楚,但是如果有一些子目录需要不同的gitignore规则,则可以在该目录中创建一个新的.gitignore。
说您有:
1 2
| project/.gitignore
project/stuff/.gitignore |
stuff /中的.gitignore将覆盖根目录中的.gitignore。
git help gitignore或man gitignore具有更多关于可能的信息。
- 我认为OP正在询问您是否具有目录结构/a/b/c,并且在/a/b中更新了.gitignore,在/a/b/c中是否有可能会忽略/a/b中的规则,而仅使用/a中的规则。
-
是的,如果这是问题所在,则可以否定.gitignore规则中的规则。 "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."
-
@signine不适用于目录。看我的答案。
-
我认为这样说更准确:inner .gitignore将与具有更高优先级的outer .gitignore合并,而不仅仅是覆盖。
-
@eric wang显然不会合并子目录,但会覆盖:git-scm.com/docs/gitignore
-
" stuff /中的.gitignore将覆盖根目录中的.gitignore。"太模糊了。我在子目录中添加了一个空的.gitignore文件,但根规则仍然适用于它。 @Nick Zalutskiy的答案奏效了。
让我们说您想在存储库中包括node_modules,但是下面的某些模块具有自己的.gitignore。您可以通过将以下内容添加到根.gitignore文件中来取消规则:
1 2
| # Make sure to include everything in node_modules.
!node_modules/** |
-
有更通用的方法可以做到这一点吗?可以说我里面有node_modules/mod1,node_modules/mod2都是.gitignore,它们忽略了其中更深的node_modules(如node_modules/mod1/node_modules一样。我经常添加和删除模块,所以我不太了解和手动添加规则以忽略node_modules/mod1/.gitignore或一直手动添加!node_modules/mod1/node_modules基本上,我正在尝试部署流星应用,并且所有嵌入式.gitignores都无法在git中将其全部获取。
此问题的关键是排除了父目录。来自$ man gitignore:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesna€?t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
这就是我让它起作用的方式。给出:
/.gitignore:
然后/b/foo将显示为包含的文件,而/a和/c中的所有内容都将被排除。需要权衡的是,您必须为要在其中包含文件的任何目录的所有父项添加一个否定项。
-
为什么不使用!/b/**/*?
-
没有您的更多解释就无法回应。
据我所知没有办法。由于本地.gitignore将覆盖根ginignore
我的解决方法是手动添加。
1
| git add --force [ your file and folder] |
-
子级.gitignore不适用于我,因为父级会覆盖所有子文件夹设置。如果您使用git add -f myfile.txt强制添加,它将绕过父.gitignore设置开始跟踪更改。