How to add an empty folder to a Git repository

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Version control systems like Git are fantastic at tracking changes to files and directories in your project. But sometimes, you may come across a peculiar situation where you want to add an empty directory into your Git repository. This can seem confusing initially, as Git does not track empty folders. However, there are workarounds to this problem, and this tutorial will provide a step-by-step guide to successfully adding empty directories to your Git repository.

Understanding Git’s Behavior with Directories

Before diving into the solution, it’s important to understand why Git doesn’t track empty directories. Git is a content tracker – which means it keeps track of file content – and since empty directories contain no files, they are not tracked. This means simply creating a directory and running git add will not preserve the directory when you commit and push your changes.

Adding a .gitignore File to Keep the Folder

The most common way to make Git recognize an empty folder is to add a .gitignore file within the folder. Here’s how you can do it:

mkdir empty_folder
touch empty_folder/.gitignore
echo "# Ignore everything in this directory" > empty_folder/.gitignore
echo "*" >> empty_folder/.gitignore
echo "# Except this file" >> empty_folder/.gitignore
echo "!.gitignore" >> empty_folder/.gitignore
git add empty_folder/.gitignore
git commit -m "Add empty_folder to keep it in repo"
git push

The commands above achieve the following:

  • mkdir empty_folder creates a new directory called empty_folder
  • touch empty_folder/.gitignore creates a new .gitignore file within that directory
  • The echo commands write rules into the .gitignore file. The rules ignore all files except the .gitignore file itself, allowing the folder to exist in the repository without any additional content.
  • The last two commands add and commit the .gitignore file to the repository, thus preserving the containing folder.

Creating a README.md for Documentation

Another approach to keep an empty folder in Git is to add a README.md file to it. This file can be helpful to document what the folder will be used for. Let’s see how this can be done:

mkdir empty_folder
touch empty_folder/README.md
echo "# Empty Folder" > empty_folder/README.md
echo "
This folder is intended for..." >> empty_folder/README.md
git add empty_folder/README.md
git commit -m "Add README for empty_folder"
git push

By adding a README.md file with descriptive text, you are effectively adding content to the directory, which Git will then track.

Using a ‘Keep’ file

Some developers prefer adding a file specifically meant to allow Git to track the directory. Often, such a file is called .keep, but its name could be anything. Here’s the process:

mkdir empty_folder
touch empty_folder/.keep
git add empty_folder/.keep
git commit -m "Add .keep file to maintain empty_folder structure"
git push

This approach has the advantage of being clear to other developers that the presence of the .keep file is only for maintaining the folder structure.

Using Git Hooks for Automation

For a more advanced scenario, you can set up a post-checkout and post-merge Git hooks to create the empty folders after every checkout or merge. Here is a simple script you can use in your hook:

#!/bin/sh
# post-checkout hook
folders="folder1 folder2"
for folder in $folders; do
  if [ ! -d "$folder" ]; then
    mkdir "$folder"
    touch "$folder/.keep"
  fi
done

This script will make sure that the specified folders and .keep files exist after operations that could potentially remove the empty folders when switching branches or merging.

Tools and Integrations

If you are working on a large project with multiple empty directories that need to be tracked, manually adding .gitignore or .keep files might not be feasible. In such cases, there are tools and integrations that can automate the process of maintaining empty directories in Git repositories, such as the use of continuous integration automations like Jenkins, Travis CI, or GitHub Actions.

Conclusion

Maintaining an empty directory structure in a Git repository requires an understanding of Git’s tracking behavior and some creative workarounds, as illustrated in this tutorial. Whether you use a .gitignore file, a README, a .keep file, or automation hooks, these methods will ensure your empty folders are recognized and maintained in version control.