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.