Let's say I have ignored a directory, but I want to unignore specific subdirectories therein. So I have the setup:
/uploads/
/uploads/rubbish/
/uploads/rubbish/stuff/KEEP_ME/
/uploads/foo/
/uploads/foo/bar/lose/
And I want to ignore everything but the KEEP_ME
directory. I would hope the ignore would look something like:
/uploads/*
!/uploads/rubbish/stuff/KEEP_ME/
But that's not working, and neither are several permutations on the same theme.
One that does work is
/uploads/**/**/**/
!/uploads/rubbish/stuff/KEEP_ME/
But that feels a little restrictive and verbose?
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/**/*
Even if you add something to .gitignore
, you can force git to add it to the index
git add --force uploads/rubbish/stuff/KEEP_ME/
However, "KEEP_ME" seems to be a directory and git usually doesnt like empty folder, so you should can add a "placeholder"-holder file instead, if the folder is empty
git add --force uploads/rubbish/stuff/KEEP_ME/.keep_me
Was trying to figure out how to include a specific folder when excluding all folders with the generic exclusion
**/build
if you add the /*
to the end of your generic exclude you continue to exclude all build files **/build/*
Then you add another line to correct for the path that you want to be included to look like
!**/folder/build/*
leaving us a gitignore that reads
**/build/*
!**/folder/build/*