Why is it not possible to store an empty directory in git?

Question:

As the title says: Why is it not possible to store an empty directory in git?

For example, in a new repo:

$ mkdir foo
$ git add foo
$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

The empty directory foo is not in the changelog.

Answer:

I found this in the Git FAQ (translated):

Currently the Git design only allows file listing. No one competent enough has bothered to make the necessary changes to allow empty directory management.

Directories are created automatically when files are added to that directory. This means that the directories are not registered by themselves.

With the command "git add <dir>" the directory files will be added.

If it is necessary to have a directory in your checkouts, you have to contain at least one file. .gitignore can help you in this task by automating the job. You can leave it empty or fill it with the names of the files that you don't want to show in the directory.

And this answer , elaborate in the .gitignore option, and secure that content:

 # Ignorar todo en este directorio
 *
 # Excepto este archivo
 !.gitignore

It is also usual to use the .gitkeep file inside the directory if it is really necessary to create the directory and version it, inside the file you can write briefly why you need to keep the versioned directory even if you do not have any other file apart from the .gitkeep . This is just a convention, for git this file has no special meaning.

Scroll to Top