Search code examples
gitgithubgitignore

what does "!" do in gitignore files?


I don't quite understand this

I've read the official documentation on it from https://git-scm.com/docs/gitignore

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. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

I have this gitignore file

/media/*
!/media/xmlconnect

What does this do exactly? I'm ignoring all subdirectories inside /media/, but am I making an exception to /media/xmlconnect (e.g. I'm NOT gitignoring it?)

so basically I'm gitignoring everything but media/xmlconnect?


Solution

  • Exactly, this is used to un-ignore a path that is ignored before that line. Your example illustrates the use case pretty good.

    You are ignoring everything inside media except xmlconnect.