How To Add An Empty Folder In Git Using .gitkeep?
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 😎.
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 README.md 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
!.gitignore
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.
Here are some other Git tutorials that I wrote (if you are interested):