是否可以忽略子目录中的.gitignore 规则?

我想忽略子目录中的所有.gitignore 规则,除了 root 目录中的.gitignore 规则。

例如: 我已经在目录结构 /a中包含了 .gitignore文件。

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

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

/a/b中的 .gitignore将忽略 .config文件。

但是我确实希望通过忽略子目录中的 .gitignore规则来跟踪 .config文件。

有可能吗?

81752 次浏览

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.

Say you have:

project/.gitignore
project/stuff/.gitignore

The .gitignore in stuff/ will override the .gitignore in the root directory.

git help gitignore or man gitignore has quite a bit more information about what's possible.

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:

# Make sure to include everything in node_modules.
!node_modules/**

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.

git add --force [ your file and folder]

The key to the problem here is that a parent directory is excluded. From $ 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.

Here's how I've gotten it to work. Given:

/a/...
/b/foo
/c/...

/.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.