2

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?

x3ro
  • 329
  • 1
  • 5
  • 11
  • Why do you want to commit an empty directory to git at all? Git doesn't track folders (at all) it tracks files – Liam Mar 14 '18 at 10:31
  • There is no right or wrong way because [none of this is pure GIT](https://stackoverflow.com/a/7229996/542251). Like I said, GIT is a file tracking system, it doesn't track directories at all. This is by design. `.gitkeep` is just a way to hack the file structure that someone made up. Do whatever works best for you. – Liam Mar 14 '18 at 10:37
  • Thanks @Liam. I already know that answer you linked to but it doesn't solve the question I have. Maybe I should reword it to "What is the best way to add a file inside an ignored directory?" – x3ro Mar 14 '18 at 10:58
  • Regarding your question why I want to commit empty directories: Some applications create folders they need, others fail if a directory they expect does not exist. I know I could create a build script that creates all necessary directories but that would sometimes simply be an overkill. Especially for small project (think of a simple web site doing something with JavaScript) it often does not need more than a `git clone` to deploy. But if let's say nginx should write logs to a `logs` directory inside my project, I need this directory to exist because otherwise nginx would fail reloading config. – x3ro Mar 14 '18 at 11:06
  • 1
    "Best way" is subjective. You have two methods that work now and you've highlighted the pros and cons of each. I'm not sure what anyone else here can add to this? You seem to be after someone to confirm your thinking but really that's your decision to make.The only question I can see is "are there any side effects for force adding a .gitkeep* to which the answer is no – Liam Mar 14 '18 at 11:06
  • Yes, you're right, I hoped for confirmation but equally for someone outlining a better solution that I didn't thought of. If there are no side effects as you say, I'll give it a try and will see which method fits me better. Thanks for your comments @Liam, really helped me :) – x3ro Mar 14 '18 at 11:18

1 Answers1

1

maybe the force add method would be better. But are there any side effects doing so?

I use the force add myself, to avoid having to maintain specific rules in .gitignore.

The only side effect is to make sure the .gitignore does not ignore too much by default, but for that, I always can count on git check-ignore to explain what is or is not ignored:

gitcheck-ignore -v -- aFile
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250