Let's toolify!
As @Joakim said, to ignore a file, you can use something like below.
# Ignore everything
*
# But not these files...
!.gitignore
!someFile.txt
but if the file is in nested directories, it's a little difficult to write those rules.
For example, if we want to skip all files but not a.txt
, located in aDir/anotherDir/someOtherDir/aDir/bDir/cDir
.
Then, our .gitignore
will be something like this
# Skip all files
*
# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
This is quite hard to write by hand.
To solve this hurdle, I've created a web app named git-do-not-ignore which will generate the rules for you.
Tool
Demo
Sample Input
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
Sample Output
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt