Gitignore: Excluding Everything Except a Specific Directory
I have the following directory structure:
|
|
My intention is to keep the projects in src/github.com/realguess/
only. So, I have added the following gitignore file:
|
|
But the directory is ignored:
|
|
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.[^1]
Uh-oh! The directory github.com/
is excluded. Therefore, it is not possible to re-include a file github.com/realguess
(directory is a file). To fix it, just need to add the wildcard to the end of the parent directory:
|
|
By adding the wildcard character, it matches every file (including hidden ones) or directory under github.com/
directory, the directory itself as the parent of github.com/realguess/
is not excluded. Then, we use the negation pattern to re-include github.com/realguess/
.
Double check it:
|
|
The debugging command returns non-zero status code, which indicates that the directory is not ignored.
We can localize the problem by moving .gitignore
file into github.com/
directory:
|
|
The ignore file contains:
|
|
Less typing on github.com
. The only caveat is to add back .gitignore
file.
The general pattern to exclude everything but a specific directory:
- Localize the gitigore (personal preference)
- Use the wildcard
- Negate
|
|
[^1]:Pattern Format, gitignore