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
    Redacted Font

    Back when I created client websites, one of the many things that frustrated me was the initial design handoff.  It would always go like this: Work hard to incorporate client's ideas, dream up awesome design. Create said design, using Lorem Ipsum text Send initial design concept to the client...

  • By
    Background Animations Using MooTools

    One of the sweet effects made easy by JavaScript frameworks like MooTools and jQuery is animation. I ran across this great jQuery tutorial that walks you through animating a background image of a page. Here's a quick MooTools code snippet that...

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!