Git doesn’t like empty folders. It doesn’t include them in commits and it certainly doesn’t include an empty directory when you push to GitHub or GitLab. But sometimes that causes a problem, which is where the .gitkeep file comes in.
What is .gitkeep?
The purpose of the .gitkeep file is to solve problem of Git not pushing empty folders to remote DVCS repos like GitHub or GitLab. To get Git to recognize an empty directory, the unwritten rule is to put a file named .gitkeep in it. Git will see the .gitkeep file in the otherwise empty folder and make that folder part of the next commit or push.
Of course, the use of a .gitkeep file is a bit of a hack. It’s certainly a work-around and it definitely doesn’t directly address the problem. After all, if an empty folder has a .gitkeep file in it, then it’s not really an empty directory anymore is it? The empty folder will get included in a Git commit or push, but only because it’s not really empty. But regardless of the logical contradiction the .gitkeep file presents, it has become a well recognized Git convention.
Not a standard
It’s worth noting that unlike the .gitignore file or the .git folder, the .gitkeep file is not part of the Git tool, the Git standard or any Git API. It’s kind of like the hidden menu at In-N-Out Burger. For regular users of the tool the use of the .gitkeep file is well known, but it’s certainly not part of the official documentation.
Maybe on some future release the .gitkeep file will be officially supported, but for now there is nothing particularly special about it.
How to use the .gitkeep file
To use the .gitkeep file to have Git push empty directories, follow these steps:
- Create an empty folder with the mkdir command
- Move into the empty directory with the cd command
- Create the .gitkeep file with the touch .gitkeep command
- Use git add . to update the Git index
- Perform a git commit
- Push the commit with the empty Git directory to remote repositories such as GitHub or GitLab
[email protected]:~$ mkdir empty-directory [email protected]:~$ cd empty-directory [email protected]:~$ touch .gitkeep [email protected]:~$ git add . [email protected]:~$ git commit -m "Commit empty folder in Git with gitkeep" [email protected]:~$ git push origin
That’s how easy it is to make Git include empty directories in a commit or push.