How do I convince git that I really do want an empty directory?
You can't. See 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.
You can say "git add <dir>" and it will add 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; you can leave it empty, or fill in the names of files you expect to show up in the directory.
Andy Lester is right, but if your directory just needs to be empty, and not empty empty, you can put an empty .gitignore
file in there as a workaround.
As an aside, this is an implementation issue, not a fundamental git storage design problem. As has been mentioned many times on the git mailing list, the reason that this has not been implemented is that no one has cared enough to submit a patch for it, not that it couldn’t or shouldn’t be done.
You can't. This is an intentional design decision by the Git maintainers. Basically, the purpose of a Source Code Management System like Git is managing source code and empty directories aren't source code. Git is also often described as a content tracker, and again, empty directories aren't content (quite the opposite, actually), so they are not tracked.
When you add a .gitignore file, if you are going to put any amount of content in it (that you want git to ignore) you might want to add a single line with just an asterisk (*
) to make sure you don't add the ignored content accidentally.
Let's say you need an empty directory named tmp:
$ mkdir tmp
$ touch tmp/.gitignore
$ git add tmp
$ echo '*' > tmp/.gitignore
$ git commit -m 'Empty directory' tmp
In other words, you need to add the .gitignore file to the index before you can tell Git to ignore it (and everything else in the empty directory).
Another way to make a directory stay empty (in the repo) is to create a .gitignore that contains two lines:
*
!.gitignore
Then you don't have to get the order right the way that you have to do in m104's solution.
I've been facing the issue with empty directories, too. The problem with using placeholder files is that you need to create them, and delete them, if they are not necessary anymore (because later on there were added sub-directories or files. With big source trees managing these placeholder files can be cumbersome and error prone.
This is why I decided to write an open source tool which can manage the creation/deletion of such placeholder files automatically. It is written for .NET platform and runs under Mono (.NET for Linux) and Windows.
Just have a look at: http://code.google.com/p/markemptydirs
Best regards and have fun with it :)
Jonny Dee
I faced this problem when trying to use git as a backup tool with support for deduplication and compression.
My solution was to create my own system. It's available on http://github.com/meingbg/store