git - .gitignore exclusion is not working for a single file -


$ git --version git version 2.6.4 

i realize duplicate other question's answers have not helped.

folder structure

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

i want ignore in dist except samples , readme.md. first few tries @ didn't work, settled on unignoring readme:

dist !dist/samples/readme.md  

readme brand new file. dist folder not in source control @ all. when 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 order of ignores
  2. removing slash beginning of line
  3. adding ** in middle: dist/**/readme.md
  4. adding /* end of dist on first line

the other things being ignored *.js, *.js.map, temp , node_modules. funny thing webstorm thinks file not being ignored (it changes color) command line tool does think being ignored.

i don't see i'm doing wrong. pattern seems simple:

  1. ignore things in directory
  2. don't ignore 1 thing here

but it's not working.

the problem because ignoring directory dist. git no longer directory other files.

as explained in this related answer, need whitelist directory , ignore contents. since having nested structure, end being bit complicated though:

# ignore in `dist` folder dist/* # … don’t ignore `dist/samples` folder !dist/samples/ # … ignore inside `dist/samples` folder dist/samples/* # … except `readme.md` there !dist/samples/readme.md 

this explicitly mentioned in gitignore documentation (emphasis mine):

an optional prefix "!" negates pattern; matching file excluded previous pattern become included again. it not possible re-include file if parent directory of file excluded. git doesn’t list excluded directories performance reasons, patterns on contained files have no effect, no matter defined.


Comments