9

I'm well aware that git doesn't work on empty directories (one of the more popular git questions here), but I'm asking if there's a particular justification or rationale (official if possible) in not tracking directories.

I'm asking because directories, in some cases, can have meaningful metadata by themselves. For example, the file mode of a directory can be important if sticky modes are being used. The mere fact that a directory is empty may be important for an application.

nneonneo
  • 171,345
  • 36
  • 312
  • 383

3 Answers3

12

There is no official rationale. The Git FAQ simply states:

nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it

Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
5

Git tracks content, folders/directories are not content. Although you are right some metadata could be interesting for you. In such a case it's recommended to add a .gitignore file or some other meaningless file to the directory in order to provide a content that git will recognise.

See How can I add an empty directory to a Git repository?

It's just the way Git was designed to work.

In some web projects I used to do I needed a log directory for my php which was of course also ignoring the .log files, so in essence I had an empty log directory which was required for Apache to run correctly due to my vhost settings.

In this case I added a readme.txt with nothing inside.

I'm sure you've already come across this answer on the other thread, still worth a link: https://stackoverflow.com/a/115992/662605

Community
  • 1
  • 1
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • 2
    Putting a file inside the directory won't preserve the mode/metadata of the directory, though. All you'll be doing is ensuring that the directory is created during `git clone` – benzado Nov 27 '12 at 18:39
  • 1
    I know that Git is a content tracker, but it can be a content tracker and still track directories...I would like to find out why it was specifically designed this way. – nneonneo Nov 27 '12 at 18:41
  • Good point. Your program would need to be aware that a ghost file is present if you need to use git for such structures I guess. – Daniel Nov 27 '12 at 18:42
  • It won't hold any data in the folder, but sometimes tools fail when the directories aren't created and hence it's sometimes good to ensure that the directory structure replicates itself when cloning. – Gustavo Litovsky Nov 27 '12 at 19:32
  • 2
    +1, but the more common way to keep an empty directory is to add a `.gitignore` file to it. Some wags have started using an empty file called `.gitkeep` file for this purpose. But I think it's an abomination. Just use a `.gitignore` which is a single common Git file and you can add local ignores in the file if you need it. – Abizern Nov 27 '12 at 19:38
4

Git is the "stupid content tracker." Much of its design is based around being "stupid"; that is, having a fairly simple (and thus general) internal representation, and relying on tools built around that to do anything fancy. It is also designed only for tracking source code; it's not designed to allow you to track arbitrary directory trees with arbitrary permissions, because that would get you into a whole rats nest of problems that is not relevant to tracking source code (do you store user and group ids? names? hard links? xattrs? resource forks? and so on).

Part of being stupid is just tracking the content of files. It does happen to store a few pieces of metadata that are essential to working on source code in a Unix environment: symlinks and the executable bit. There are enough scripts that people check in, that you want to track the executable bit, because otherwise everyone is going to have to implement that. But besides those two pieces of metadata, Git just keeps track of the contents of files and their names.

If you have use-cases beyond that, you're expected to create your own porcelain, wrap the existing one, or use the extension points that Git provides like hooks and attributes.

If you want richer tracking of permissions and directories, I would recommend using a wrapper like etckeeper.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340