Telling git to track an empty folder

3 min. read

Caution! This article is 7 years old. It may be obsolete or show old techniques. It may also still be relevant, and you may find it useful! So it has been marked as deprecated, just in case.

I'm working on a new personal project, were I'll share my C# snippets. As all my personal projects, it started as a reference I wanted to keep for myself, he he.

There is this one snippet that is about creating a console file server, and the server listens to files appearing in a folder called queue.

To ship this snippet, I would like to do it including the queue folder, even though it would be empty. I want to show you the folder structure without having to add "remember to create a queue folder here" in the tutorial. Call me silly! It just seems more elegant to me to give you the whole thing in one pack.

But hadn't I wanted to do things like this, I wouldn't have learned a new thing today, which is what I want to share below.

Git doesn't track empty folders

The problem is that git won't track an empty folder, for very reasonable reasons explained very clearly by Linus himself.

Basically (I quote):

  • "You cannot (and should not) track directories the same way as you track a file".
  • "When you track an empty file, you very much track the *content* of that file, and "empty" just happens to be a very valid content".
  • "When you track a "directory", you don't actually track its content at all, you track it's *existence*, which is a very very very different thing".

How to track an empty folder, then?

There are several ways:

  • You could add a .gitignore file, where you include the folder. But using the ignore to not ignore feels weird.
  • You could add an empty README file. But why add files to your repo that you don't need?
  • You could add a .keep file. This is my favorite, because it's still a hidden file, but its name is more in line with what it actually does: to tell git to keep it.

So, from now on, every time I want to tell git to track an empty folder, I'll throw a .keep file in, and play a violin chord.