Tracking empty directories with Git

On November 13, 2011, in Git, by Anuj Gakhar

Those familiar with Git, will know that Git only tracks files and not folders. What that means is, if a folder is empty (does not have any files in it), Git will totally ignore it, as there is nothing to track as far as Git is concerned. Now, sometimes, we do run into situations where we want to include a folder in the project directory tree, even if it’s empty. Consider a folder for storing user uploaded content, it could be empty to begin with. To make matters worse, this uploads folder could have different contents on different environments. I could be testing with test images on local setup and the production environment would have real uploads. You get the idea…there can be many more examples.

Anyways, what I’ve been doing is to add a .gitignore file to this empty folder with contents as follows :-

[xml]# Ignore everything in this directory
*
# Except this file
!.gitignore[/xml]

This will ensure that directory stays empty and will always be tracked in the Git repo.

There is one other way of tracking empty directories that I have seen some people follow, and that is to add a file called .gitkeep in their empty directories. Technically, since the directory is not empty anymore, Git will track it.

I am more in favour of option #1, the once that I am already doing.

Thoughts?

Tagged with:  

3 Responses to Tracking empty directories with Git

  1. James Brown says:

    I personally think the .gitignore option is better if you want the directory to always stay empty as far as git is concerned. If you just want the folder to exist (say for a template or a project base), then either would work.

  2. Anuj Gakhar says:

    Agreed James, I tend to use .gitignore as well most of the times.

  3. Meaning of Statistics: The science of producing unreliable facts from reliable figures.
    Blessed is he who may have found his work; let him ask few other blessedness.

Leave a Reply to James Brown Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: