I'm using git very intensively for quite some time now and I'd say that I have a quite good understanding of how git works under the hood but there is one question that I couldn't find a satisfactory answer to:
"What is the best way to track an empty directory?"
I know there are discussions about whether you should place a .gitignore
(which is a documented file in git) or a .gitkeep
file (which seems to be just a convention) inside the directory. But that should not be the question.
I'm a fan of the .gitkeep
for some reasons:
- I like having all ignore rules in one file and using
.gitkeep
prevents from someone putting ignore rules in sub directories. - For me it's more like saying 'ignore the contents but keep this directory' than 'ignore this director but hey, you need it so do not ignore it completely' (although that's what you are doing when you whitelist the
.gitkeep
..)
I'm aware of two techniques to achieve what I want:
- ignore the directory contents, whitelist the
.gitkeep
file and add it as usual - ignore the whole directory but force add the
.gitkeep
file.
Until now I used the whitelist method but I had trouble with it now and then especially when working in a team and someone desides to ignore a parent directory of a directory that is already ignored:
/some/path/to/ignore/aaa/**
!/some/path/to/ignore/aaa/.gitkeep
/some/path/to/ignore/bbb/*
/some/path/to/ignore/bbb/.gitkeep
# ... somewhere later in the file ...
/some/path/to/ignore
I know the directories aaa
and bbb
are ignored and when I add ccc
I add the ignore / whitelist rule. But I can't see the .gitkeep
file now because of the last rule.
So I thought maybe the force add method would be better. But are there any side effects doing so?