.gitignore: permit only certain paths with negation
May 15th, 2014 (edited November 3rd, 2022)tutorial
git
Today I was trying to create a .gitignore file for a project that has to live in the same directory as a number of different folders. I wanted the git repo to only include folders that start with the string heroscape, while excluding any other folder.
The gitignore man pages point out that you can negate a pattern by putting a ! in front of it. I didn't notice at first that this does not ignore anything that does not match the pattern. Instead, it re-includes any previously ignored paths that match the negated pattern.
So to achieve what I wanted, I had to do the following, first ignoring all folders, then re-including folders that begin with "heroscape".
*/
!heroscape*/