Git: How do I add an empty directory to a Git project/repository?

Git empty directories FAQ: How do I add an empty directory to a Git repository?

Short answer: you can't. The solution is to add a dummy file to a Git directory, which is while you’ll sometimes see files named dummy, empty, placeholder, etc. The design of the Git staging area only accounts for files, as described in the Git FAQ, and other books like Pro Git.

Read on for more details ...

Git empty directories FAQ

Here's the text from the Git FAQ section, "Can I add empty directories to a Git repository":

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. You can say "git add <dir>" and it will add the files in there.

If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose (there is also a tool MarkEmptyDirs using the .NET framework which allows you to automate this task); you can leave it empty or fill in the names of files you expect to show up in the directory.

As you can see, this "Git empty directories" problem isn't going away any time soon, so at the moment the solution to the problem is a simple workaround.

Git empty directory workaround

If you happen to work with CakePHP, and you don't already know about this Git empty directory problem, you'll learn about it quickly when you look in the default plugins or vendors directories. There you'll find files named "empty" that have nothing in them. Of course now that you know about this Git empty directory problem, you know that these files are placeholders to convince Git to check these directories into the Git repository.

So that's the Git empty directories solution for now: Create a placeholder file in your empty Git directories to convince Git to put the empty directory into your Git repository. (For more information on this problem, here's a link to the Git FAQ at kernel.org.)