Git and empty folders

Since it is well known that Git does not know any folders but only files, empty folder structures do not end up in the repository. However, since many frameworks and projects absolutely need 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, if desired, can also ensure 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

A git status -u shows us no changes as expected:

Git and empty folders

We 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 to ensure that the folder structure always ends up in the repository, but not any files (apart from the placeholder files), we create a gitignore:

434e6616f335599d241482f169f68f1b

We use the double star syntax :

434e6616f335599d241482f169f68f1b

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

Now we create some test files:

434e6616f335599d241482f169f68f1b

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

Git and empty folders

Back