Keep an empty folder in Git history

NicolasBrondinBernard

Author
@NicolasBrondinBernard

By default, an empty folder will not be tracked by Git. Here are two solutions to work around this constraint!

Article published on 16/05/2022, last updated on 10/08/2026

Sometimes in a project you may need to have one or more folders that must be present but empty at project initialization, but which will be filled by the application itself (such as uploads, data, save, etc.).

Except that the Git version control system only considers history in the form of files, so an empty folder will never be added to the current version, even when using "git add ."

The absence of these folders may then cause a problem when running the application. Here are two methods to avoid this inconvenience:

Method 1: A .gitkeep file

The first alternative to keep an empty folder in Git is for it to not be... empty. Indeed, all you need to do is add an empty file in the folder in question and add it to the Git history.

There is a convention of naming this file .gitkeep

This convention is not listed in the Git documentation, but it is nevertheless taken into account by some clients.

Method 2: A .gitignore file

If you'd like to use something documented, and offering a few extra possibilities, then you can add a .gitignore file in your empty folder.

You will then have the option to tell Git that this folder must always remain empty (aside from the .gitignore).

# Ignore all files in this directory
*

# Except this one
!.gitignore

Bonus method

As you'll have understood, with these two methods, your folder won't really be empty anymore. If you need there to be absolutely no data in this folder, then you will simply need to create it automatically every time your application starts.

As a result, your folder won't even need to be versioned anymore, since it will be generated whenever needed!


Jay Heike sur Unsplash

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet