Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore exclusion is not working for a single file

Tags:

git

bash

macos

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

like image 968
jcollum Avatar asked Feb 08 '16 20:02

jcollum


2 Answers

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.

like image 84
poke Avatar answered Sep 16 '22 19:09

poke


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.

like image 22
Pavel Avatar answered Sep 19 '22 19:09

Pavel