Categories
directory git git-add

How do I add an empty directory to a Git repository?

5011

How do I add an empty directory (that contains no files) to a Git repository?

11

  • 20

    While it’s not useful, there is a way to hack an empty (really empty) directory into your repo. It won’t checkout with current versions of Git, however.

    – tiwo

    Jul 22, 2012 at 14:18

  • 420

    @tiwo I for one disagree that it’s not useful. Your directory hierarchy is part of your project, so it should be version controlled.

    – JBentley

    Jan 29, 2013 at 20:19

  • 150

    In my case, I’d like to add a directory structure for tmp files, but not the tmp files themselves. By doing this, my tester has the correct structure (otherwise there are errors) but I don’t clog my commits with tmp data. So yes, it’s useful to me!

    Mar 13, 2013 at 3:32


  • 50

    @AdamMarshall I think tiwo was saying that the hack is not useful, since it is ignored by checkout. Tmp dirs do sound like a useful feature for a VCS.

    – Quantum7

    Apr 22, 2013 at 21:33

  • 34

    Why not have the procedure that creates the tmp files also create the tmp directory?

    – RyPeck

    Jul 9, 2013 at 3:11

4811

Another way to make a directory stay (almost) empty (in the repository) is to create a .gitignore file inside that directory that contains these four lines:

# Ignore everything in this directory
*
# Except this file
!.gitignore

Then you don’t have to get the order right the way that you have to do in m104’s solution.

This also gives the benefit that files in that directory won’t show up as “untracked” when you do a git status.

Making @GreenAsJade‘s comment persistent:

I think it’s worth noting that this solution does precisely what the question asked for, but is not perhaps what many people looking at this question will have been looking for. This solution guarantees that the directory remains empty. It says “I truly never want files checked in here”. As opposed to “I don’t have any files to check in here, yet, but I need the directory here, files may be coming later”.

24

  • 38

    I think the README solution proposed by @JohnMee should be used together with this one; the .gitignore file provides an explanation of what we want to keep out of version control, while the README file explains what is the purpose of the directory, which are both very important pieces of information.

    Jan 17, 2013 at 11:11

  • 24

    @pedromanoel I write the documentation you would put in the README inside the .gitignore file (as comments).

    Jul 19, 2013 at 8:20

  • 90

    spot the 1 difference: 1.) an empty folder, 2.) a folder with .gitignore file in it. 😉

    Feb 11, 2014 at 14:31

  • 11

    This is perfect for cache folders.

    – redolent

    Mar 26, 2014 at 2:30

  • 17

    Unfortunately, this results in a non-empty directory, it has a single hidden file.

    – pedorro

    Dec 15, 2014 at 20:09

1177

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.

7

  • 81

    Below answer is MUCH better. The fact that git the low level software doesn’t allow it doesn’t matter to me as much as HOW to actually use Git when I need an empty directory. Adding a 2 line .gitignore seems acceptable to me.

    – Amala

    Apr 26, 2011 at 15:21

  • 18

    You can read “it’s impossible, you can’t, etc.” all over the Internet for this frequent question. The .gitignore trick is a frequent answer, and satisfies many needs. However it IS possible to make git track an truly empty directory, see my answer

    – ofavre

    Jan 21, 2012 at 15:44


  • 2

    Though the more I think of it, the more it feels like “SHA hash of the empty string”, if it exists, actually would be a well-defined identifier for an empty tree, unless it would be impossible to tell whether that object is a tree or a blob.

    Jul 9, 2013 at 9:47

  • 28

    I’ve seen a lot of repos that use an empty file called .gitkeep for this purpose.

    – Sukima

    Nov 13, 2013 at 1:38

  • 4

    @Amala: what are you referring to, when you say “below”. The order of answers is changing…

    Dec 11, 2017 at 12:42

991

Create an empty file called .gitkeep in the directory, and add that.

10

  • 266

    .gitkeep has not been prescribed by Git and is going to make people second guess its meaning, which will lead them to google searches, which will lead them here. The .git prefix convention should be reserved for files and directories that Git itself uses.

    – t-mart

    Feb 10, 2014 at 1:44

  • 15

    @t-mart “The .git prefix convention should be reserved…” Why? Does git request this reservation?

    Aug 28, 2014 at 18:13

  • 18

    In this case a README or ABOUT file would be just as good or better. Leaving a note for the next guy, just like we all used to do it before URLs.

    – Dave

    Nov 15, 2014 at 0:59

  • 10

    Doesn’t work if you’re writing a unit test that should test code on an empty directory…

    – thebjorn

    Dec 23, 2015 at 10:22

  • 10

    @szablica I don’t think it’s confusing at all. In fact I think it’s very intuitive to call it .gitkeep. Calling it .gitignore is what sounds contradictory to me. So this is just a matter of personal taste.

    – Mig82

    May 15, 2017 at 18:17