Explore Courses Blog Tutorials Interview Questions
+2 votes
in DevOps and Agile by (19.4k points)

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

1 Answer

+1 vote
by (27.5k points)
edited by

Note: An empty directory cannot be part of a tree under the Git versioning system. An empty directory simply won't be tracked. But there are cases where 'versioning' empty directories can be meaningful. 

Here's an example of such scenarios:

  • Scaffolding a predefined folder structure, making it available to every user or contributor of the repository; or, as a specialized case of the above, creating a folder for temporary files, such as logs/ or a cache/ directory, where we want to provide the folder but .gitignore its contents.
  • Related to the above point, some projects won't work without some folders (which is often a hint of a poorly designed project, but it's a frequent real-world scenario and there could be permission problems to be addressed as well).

A very common suggestion is to place a README file or another file with some content in order to make the directory non-empty, or

Another way is to create a .gitignore file with a sort of 'reverse logic' (i.e. to include all the files) which, at the end, serves the same purpose of approach #1.

While both solutions surely work I find them inconsistent with a meaningful approach to Git versioning. 

  • Why would you even put bogus files or README files that maybe you don't really want in your project?
  • Why would you use .gitignore to do a thing (keeping files) that is the very opposite of what it's meant for (excluding files), even though it is possible? 

Here's another way, .gitkeep approach:

In this approach, we will use an empty file called .gitkeep in order to force the presence of the folder in the versioning system. 

  • Although it may seem not such a big difference, using a file that has the single purpose of keeping the folder. You don't put there any info you don't want to put. 
  • Naming it .gitkeep makes it very clear and straightforward from the filename itself (and also to other developers, which is good for a shared project and one of the core purposes of a Git repository) that this file is
    • A file unrelated to the code (because of the leading dot and the name)
    • A file clearly related to Git
    • Its purpose (keep) is clearly stated and consistent and semantically opposed in its meaning to ignore

For more information please go through the following tutorial to get more info about git:


Browse Categories