Exceptions in a .gitignore file

Download git eBook

Example

If you ignore files by using a pattern but have exceptions, prefix an exclamation mark(!) to the exception. For example:

*.txt
!important.txt

The above example instructs Git to ignore all files with the .txt extension except for files named important.txt.

If the file is in an ignored folder, you can NOT re-include it so easily:

folder/
!folder/*.txt

In this example all .txt files in the folder would remain ignored.

The right way is re-include the folder itself on a separate line, then ignore all files in folder by *, finally re-include the *.txt in folder, as the following:

!folder/
folder/*
!folder/*.txt

Note: For file names beginning with an exclamation mark, add two exclamation marks or escape with the \ character:

!!includethis
\!excludethis

Stats

Contributors: 12
2016-09-13
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook