Unignore subdirectories of ignored directories in Git

According to pattern format section of the gitignore documentation:

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”.

Therefore the previously-excluded parent directory /uploads/rubbish/stuff/keep/ pattern must be exclusively negated before negating its content:

#ignore everything within /uploads/ 
/uploads/*

#include everything within /uploads/rubbish/stuff/keep
!/uploads/rubbish/stuff/keep/  
!/uploads/rubbish/stuff/keep/*

To include subdirectories inside /uploads/rubbish/stuff/keep/ add the third line:

!/uploads/rubbish/stuff/keep/**/*

Leave a Comment