Exceptions in a .gitignore file

You can tell Git to ignore some files or directories using a pattern. However, there are some exceptions we don’t want miss files which are matched to the pattern. In this post, I am going to briefly present how to use an exclamation mark syntax.

*.txt
!readme.txt

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

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

logs/
!logs/debug.log

In this example, debug.log file in the folder would remain ignored.

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

!logs/
logs/*
!logs/*.txt

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

!!includethis
\!excludethis

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.