Use .gitkeep to commit & push an empty Git folder or directory

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:

  1. Create an empty folder with the mkdir command
  2. Move into the empty directory with the cd command
  3. Create the .gitkeep file with the touch .gitkeep command
  4. Use git add . to update the Git index
  5. Perform a git commit
  6. Push the commit with the empty Git directory to remote repositories such as GitHub or GitLab
gitkeep@example:~$ mkdir empty-directory
gitkeep@example:~$ cd empty-directory
gitkeep@example:~$ touch .gitkeep
gitkeep@example:~$ git add .
gitkeep@example:~$ git commit -m "Commit empty folder in Git with gitkeep"
gitkeep@example:~$ git push origin

That’s how easy it is to make Git include empty directories in a commit or push.

what is gitkeep

How to use .gitkeep to commit or push an empty Git folder or empty Git directory.

App Architecture
Software Quality
Cloud Computing