How to commit an empty directory to Git

There is no way to commit an empty directory to a Git repository, you have to put a file in the directory to be able to commit it to your repo. Here is an explanation from the Git FAQ:

Currently the design of the Git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.
Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

Possible paths

So now we know that we have to put a file in the directory. But what file should it be?

If we are forced to put in a file we should make the best of it. Use a file that has at least some use, or could have a use in the future.

Use a README file

Some say, that the best idea is to use a README file, and inside it, describe why did you leave this directory empty. This could be a good idea, but sometimes the reason for the directory being there is obvious (e.g. Controllers), you just don’t have any files in it, because you did not get to that point in the development.

Use a .gitignore file

Git uses the .gitignore file to determine which files to exclude from tracking in the repository. Putting in an empty one leaves the possibility of adding exclusion rules later.

Use a .gitkeep file

In some people’s opinion the filename should be descriptive about the purpose of it. They use a file named .gitkeep, that just means “Keep this folder in revision control, although it’s empty”.