122

I want to ignore all of .gitignore rules in subdirectory except .gitignore rules in root.

for example: I already have .gitignore file in a directory structure /a.

And also have .gitignore file in /a/b. Assume /a has a.txt and b.txt files.

/a/b has .config file. .gitignore defines .config in /a/b.

.config file will be ignored by .gitignore in /a/b.

But I really want to track .config file by ignoring rules of .gitignore in subdirectory.

Is it possible?

4 Answers 4

182

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.

6
  • 5
    I think the OP is asking if you have a directory structure /a/b/c and you update .gitignore in /a/b, is it possible in /a/b/c to ignore the rules in /a/b and instead only use rules from /a. Commented Sep 15, 2010 at 21:32
  • 57
    Yeah, if that was the question, it is possible to negate rules in a .gitignore further down the chain. "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."
    – signine
    Commented Sep 16, 2010 at 0:38
  • 3
    @signine Doesn't work with directories. See my answer. Commented Mar 19, 2016 at 23:01
  • 7
    I think it's more accurate to say that: the inner .gitignore will merge with outer .gitignore with higher priority, not simply override.
    – Eric
    Commented Feb 7, 2020 at 0:55
  • @eric wang apparently a subdirectory does not merge, but overrides: git-scm.com/docs/gitignore
    – topspin
    Commented May 10, 2020 at 15:11
61

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/**
1
  • 5
    Is there a more generic way to do this? lets say I've got node_modules/mod1, node_modules/mod2 inside both are a .gitignore that ignores the deeper node_modules inside those (as in node_modules/mod1/node_modules. I add and remove modules often so I can't very well know and manually add rules to ignore node_modules/mod1/.gitignore or manually add !node_modules/mod1/node_modules all the time. Basically I'm trying to deploy a meteor app and all the embedded .gitignores are making it impossible to get it all in git.
    – gman
    Commented Nov 6, 2014 at 4:32
14

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.

2
  • Why not use !/b/**/*? Commented May 9, 2017 at 8:14
  • Can't respond without more explanation from you. Commented May 9, 2017 at 13:12
9

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]
1
  • child .gitignore didn't work for me as the parent overrides any child folder settings. If you force add using git add -f myfile.txt it will start tracking changes bypassing the parent .gitignore settings. Commented Jun 13, 2017 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.