Track Empty Directories with git

By  on  

There are times when you'd like to track an empty directory within git but there's a problem: git wont allow you to add a directory that doesn't have a file in it.  The easy solution is putting an empty stub file within the directory, and the industry standard for that stub file name is .gitkeep.

You can quickly create the file and commit the "empty" directory from command line:

touch my-empty-dir/.gitkeep
git add my-empty-dir/.gitkeep
git commit -m "Adding my empty directory"

The problem is simple, the solution is easy, but I wanted to highlight that .gitkeep is the industry standard.

Recent Features

Incredible Demos

  • By
    dat.gui:  Exceptional JavaScript Interface Controller

    We all love trusted JavaScript frameworks like MooTools, jQuery, and Dojo, but there's a big push toward using focused micro-frameworks for smaller purposes. Of course, there are positives and negatives to using them.  Positives include smaller JS footprint (especially good for mobile) and less cruft, negatives...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Discussion

  1. Mathew

    If you want to keep empty directory in git and be sure that its eventually content won’t be pushed, you have to add line in .gitignore. Going to the point, .gitkeep is one of the methods and the more common (from my experience) is to create .gitignore with

    *
    !.gitignore
    
  2. Michal

    I’d be interested where the “.gitkeep is the industry standard” came from. Last time I was looking at a couple of repositories, the preference was an empty .gitignore file.

  3. Good tips, Mathew and David!

    @Mathew: When I use this tip, I usually include */ as well to exclude subfolders. This can be pretty handy for those log/, cache, and sessions types of directories.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!