tags:

views:

11573

answers:

8

How do I convince git that I really do want an empty directory?

+40  A: 

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
+7  A: 

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.

Aristotle Pagaltzis
That's exactly what I said. Both paragraphs are addressed in the snippet of FAQ I posted.
Andy Lester
I think the aside is unteresting and useful to know -- it can be fixed, just don't expect it anytime soon when there's such an easy workaround for most cases.
wnoise
Sorry, I didn’t read the last paragraph, and while I did read the first paragraph, well, I’m not sure why I repeated that information.
Aristotle Pagaltzis
Of course, this extra answer does serve to point out the fact.
Michael Johnson
+1  A: 

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.

Jörg W Mittag
+3  A: 

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.

Michael Johnson
A: 

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).

m104
Two things:You could just "echo '*' > tmp/.gitignore" instead of touching, and "git commit -m" does not commit changes done after you've added the files to the index.
Christoffer Hammarström
+55  A: 

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.

Jamie Flournoy
+1 - Thanks for the info. Exactly what I needed!
Topher Fangio
+1  A: 

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

A: 

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

meingbg