How do negated patterns work in .gitignore?

Chris Perkins picture Chris Perkins · May 12, 2010 · Viewed 32.5k times · Source

I am attempting to use a .gitignore file with negated patterns (lines starting with !), but it's not working the way I expect.

As a minimal example, I have the folllowing directory structure:

C:/gittest
 -- .gitignore
 -- aaa/
   -- bbb/
     -- file.txt
   -- ccc/
     -- otherfile.txt

and in my gitignore file, I have this:

aaa/
!aaa/ccc/

My understanding (based on this doc page) is that the file aaa/ccc/otherfile.txt should not be ignored, but in fact git is ignoring everything under aaa.

Am I misunderstanding this sentence: "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."?

BTW, this is on Windows with msysgit 1.7.0.2.

Answer

Cascabel picture Cascabel · May 12, 2010

I think that what you actually want to do is:

aaa/*
!aaa/ccc

You're telling it "don't look in aaa" so it never even examines the path aaa/ccc. If you use the wildcard, it still reads the contents of aaa, then each entry matches the wildcard and is ignored, except aaa/ccc which gets put back in.