How do .gitignore exclusion rules actually work?

/a/b/c/*
!foo

Seems to work for me (git 1.7.0.4 on Linux). The * is important as otherwise you’re ignoring the directory itself (so git won’t look inside) instead of the files within the directory (which allows for the exclusion).

Think of the exclusions as saying “but not this one” rather than “but include this” – “ignore this directory (/a/b/c/) but not this one (foo)” doesn’t make much sense; “ignore all files in this directory (/a/b/c/*) but not this one (foo)” does. To quote the man page:

An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again.

i.e., the file has to have been excluded already to be included again. Hope that sheds some light.

Leave a Comment