Git and empty folders

Since Git knows that it doesn't know folders, but only files, empty folder structures don't end up in the repository, but since many frameworks and projects require them, they should end up there. A common practice is to create so-called placeholder files (often called .gitkeep) that implicitly specify the folder structure. These files can be easily created and additionally, if desired, make sure that other files in the folders are ignored.


First we create a test repository and create some subfolders:

434e6616f335599d241482f169f68f1b

We now have the following folder structure:

434e6616f335599d241482f169f68f1b

As expected, a git status -u shows us no changes:

Git and empty folders

We will now use find to create the placeholder files at all required locations:

434e6616f335599d241482f169f68f1b

We have now received the following folder / file structure:

434e6616f335599d241482f169f68f1b

A git status -u now shows us that all folders (implicitly) end up in the repository:

Git and empty folders

If we want the folder structure to always end up in the repository, but not any files (except for the placeholder files), we create a gitignore:

434e6616f335599d241482f169f68f1b

For this purpose we make use of the Double star syntax:

434e6616f335599d241482f169f68f1b

The first line recursively ignores all files and folders (at any level) in the folder "foo", the second line recursively excludes from this rule all folders (at any level) and the third line excludes all .gitkeep files (at any level).

Now let's create some test files:

434e6616f335599d241482f169f68f1b

A git status -u now shows us the desired result:

Git and empty folders

Back