22
$ git --version
git version 2.6.4

I realize this is a duplicate but the other question's answers have not helped.

Folder structure

/
-- dist/
---- samples/ 
---- README.md
---- foo/
---- bar/
---- baz.js 

I want to ignore everything in dist except samples and README.md. My first few tries at it didn't work, so I settled on just unignoring the README:

dist
!dist/samples/README.md 

README is a brand new file. The dist folder is not in source control at all. But when I check ignore, it's still being ignored:

$ git check-ignore dist/samples/README.md 
dist/samples/README.md
$ git check-ignore src
   # empty line    

I've tried:

  1. changing the order of the ignores
  2. removing the slash from the beginning of the line
  3. adding a ** in the middle: dist/**/README.md
  4. adding a /* to the end of dist on the first line

The only other things being ignored are *.js, *.js.map, temp and node_modules. The funny thing is that Webstorm thinks that the file is not being ignored (it changes color) but the command line tool does think it is being ignored.

I don't see what I'm doing wrong. The pattern seems very simple:

  1. ignore all the things in this directory
  2. don't ignore this one thing here

But it's clearly not working.

3 Answers 3

64

The problem is simply because you are ignoring the directory dist. So Git will no longer look into the directory to look for other files.

As explained in this related answer, you need to whitelist the directory and only ignore its contents. Since you are having a nested structure, this does end up being a bit complicated though:

# ignore everything in the `dist` folder
dist/*
# … but don’t ignore the `dist/samples` folder
!dist/samples/
# … but ignore everything inside that `dist/samples` folder
dist/samples/*
# … except the `README.md` there
!dist/samples/README.md

This is also explicitly mentioned in the gitignore documentation (emphasis mine):

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. 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.

5
  • yes, you're right, just sorted that out; it's confusing, logically; I'm telling it to ignore all the things in a directory except the one thing but git says "no, sorry, ignoring the directory takes precedence"
    – jcollum
    Commented Feb 8, 2016 at 20:53
  • I agree that it’s a bit annoying but that’s unfortunately just how Git behaves here :/
    – poke
    Commented Feb 8, 2016 at 20:54
  • 1
    @jcollum Good find on the documenation part there, thanks :)
    – poke
    Commented Feb 8, 2016 at 21:05
  • Code comments were extremely helpful in understanding exactly what's going on here. I'm curious if there are any implications based on the git version. Commented Sep 23, 2020 at 21:02
  • 1
    @CalebAdams As far as I can tell, in at least the past 10 years, Git hasn’t changed its behavior with the gitignore syntax. So this should work with pretty much any Git version you can find.
    – poke
    Commented Sep 23, 2020 at 21:21
3

tl;dr:

/dist/**/*.*
!/dist/**/README.md
!/dist/samples/*.*

Proof it works:

tree dist -a
dist
├── bar
│   └── index.html
├── baz.js
├── foo
│   └── main.js
└── samples
    ├── README.md
    └── stuff.jpg

find dist -type f | git check-ignore --verbose --non-matching --stdin
.gitignore:1:/dist/**/*.*   dist/baz.js
.gitignore:2:!/dist/samples/*.* dist/samples/stuff.jpg
.gitignore:2:!/dist/samples/*.* dist/samples/README.md
.gitignore:1:/dist/**/*.*   dist/foo/main.js
.gitignore:1:/dist/**/*.*   dist/bar/index.html

Like https://stackoverflow.com/a/35279076/2496472 says, the docs mention:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. 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.

However, the docs also say:

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

  • A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

Putting this together, the strategy is to never ignore a directory but ignore all files within a directory, and then exclude the file you want.

References:

1
  • Using *.* is not good for files, like Dockerfile.
    – Akito
    Commented Jan 22, 2023 at 18:14
2

I has the similar problem with another .gitignore in subdirectory. For examaple:

- root
-- Subdir
--- gulpfile.js
--- .gitignore
-- .gitignore

In root .gitignore where two rules:

*.js
!gulpfile.js

And in Subdir/gulpfile.js where another one rule:

*.js

Because of that, Subdir/gulpfile.js where ignored. To solve problem it is required to remove Subdir/.gitignore, or add !gulpfile.js to Subdir/.gitignore.

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.