May 11, 2013

Keep empty directories in git: The VERY easy way

I keep hearing this question over and over in the internets: How do I keep an empty directory in git?
And they're usually followed by a hundred of different answers, mostly "you can't" and very complicated ones.
But, in fact, there is a way that's pretty EASY:

  • Create your empty directory, let's say it's temp
  • Create a .gitignore file with this content * inside the directory.
  • Force add the file to your git repository. You can do that with the -f flag.
  • That's all. Yes, no more files inside temp will be tracked automatically.

If you're in a command line, it will look like this:

~$ mkdir temp
~$ echo '*' > temp/.gitignore
~$ git add -f temp/.gitignore

Now have fun creating empty directories and track them with git!