How To Add An Empty Folder In Git Using .gitkeep?

Tim Mouskhelichvili
Tim Mouskhelichvili
2 minutes to read

If you regularly work with Git, you have surely noticed that Git doesn't like empty folders. It doesn't include them when you try to commit. Luckily, you can still add an empty folder in Git using a .gitkeep file.

To add an empty folder in Git, create an empty file named .gitkeep inside that folder.

This article will explain empty folders in Git and the .gitkeep file.

Let's get to it 😎.

git add empty folder

Why track empty folders?

In some cases, tracking empty folders inside Git is useful:

  • Some projects will not work without an empty folder.
  • To make a predefined folder structure available to every user.

Why does Git ignore empty folders?

Git doesn't include empty folders because it only cares about files.

That's why you can have a different folder structure on your local repository and the remote.

Luckily, there is still a way to stage, commit and push an empty folder.

How to add an empty folder?

Add an empty .gitkeep file inside the desired folder.

Then, using the git status command, verify that Git sees the changes.

Finally, stage the changes, commit and push to a remote.

The .gitkeep file is not a standard file in the Git software and is not part of official documentation. However, that filename is an unwritten rule that most developers follow when adding new empty folders.

Another option is to add a file inside the folder that explains why you want this folder to be empty.

How to keep a folder empty?

To keep a folder empty, utilize the .gitignore file.

Add a .gitignore file to the desired folder with this content:

bash# Ignore everything in this folder
# Except this file

This will ensure that this folder stays empty, even if you add files or folders inside.

Final thoughts

As you can see, it is not possible to truly add an empty folder in Git. You need to add a hack in the form of a file to do it.

git add empty folder

Here are some other Git tutorials that I wrote (if you are interested):

Comments (2)
Reply to:
Jamison January 19, 2023 00:10AM
Hey Tim this was great thanks :)Just a note in your section "How to keep a folder empty" I think you mean !.gitkeep instead of !.gitignore?
Tim Mouskhelichvili January 23, 2023 23:43PM
In that case, we need to put a .gitignore file instead of the .gitkeep one. This ensures the folder stays empty (even if there is stuff inside).