Git is a version control system designed to help developers keep track of the changes they make to their code. Git follows all the changes made to the local repository and allows users to push (sync) the changes with a remote repo.
When preparing the local repository for the next project, it is common practice to structure the directory's hierarchy in advance. However, Git ignores empty directories when pushing changes to a remote repo.
In this tutorial, you will learn to add and push an empty directory in Git.
- Git installed and configured (follow our tutorials and install Git on Windows, macOS, Ubuntu, CentOS 7, or CentOS 8).
- A local and remote Git repository.
How to Add an Empty Directory in Git?
Working in a local repository, open a command prompt (terminal on Linux and macOS or Git Bash in Windows), and follow the steps below to create and push an empty directory in Git.
Step 1: Create New Directory
Using the mkdir command, create a new, empty directory in your local repository. The syntax is:
For example, we will create a new directory called pnap:
Listing the contents of the repository using the ls command shows that the directory has been created:
Step 2: Move to the New Directory
Move to the new directory using the cd command:
For example, to move to the pnap directory we created in the previous step, run:
The command prompt now operates in the pnap directory.
Step 3: Add a File to the Directory
Instruct Git to track the new directory by creating a dummy file using the touch command. The purpose of the file is only to help Git recognize an empty directory, which it would otherwise ignore. A common practice is to create a .gitkeep or .placeholder file.
Such files are invisible to most systems by default but force Git to track them. Some users prefer the .placeholder file because the .git prefix convention is reserved for files and directories that Git uses for configuration purposes.
Note: Placing a .gitkeep or .placeholder file in a directory is only a community-devised workaround for enabling Git to recognize empty directories and is not described in Git documentation. The files are only a popular convention and serve no purpose other than making the directory visible to Git.
Run the following command to create the file:
The command creates an empty file named .placeholder.
Step 4: Stage File
Run the following command to stage the file and update the Git index:
git add .
Check if the file has been staged by running:
The file and the directory are now in Git's tracking index.
Note: See how to unstage a file in Git.
Step 5: Make a Commit
Commit the file and directory using the following syntax:
git commit -m "message"
message," specify a description for the commit. For example:
git commit -m "Created directory hierarchy"
Note: See how to revert to an earlier commit in Git.
Step 6: Push Changes
Push the commit with the empty Git directory to a remote repository on GitHub. Pushing allows you to share your changes with teammates in the remote repository. The syntax is:
git push [remote-name]
[remote-name], specify the name of the remote repository. For example:
git push origin
The command pushes the empty directory (and the .placeholder file) to the remote repository, making the directory visible to everyone working on the project.
This guide showed how to add an empty directory in Git and synchronize it with a remote repository. Although the method is only a workaround created by the community, it helps developers prepare the project by structuring it in advance.
Learn more about Git in our how Git works tutorial, or learn to use Git effectively with our beginner's guide.