How to Add an Empty Directory in Git

October 26, 2022


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.

How to add an empty directory in Git


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:

mkdir [directory-name]

For example, we will create a new directory called pnap:

mkdir pnap

Listing the contents of the repository using the ls command shows that the directory has been created:

Creating a new directory in Git.

Step 2: Move to the New Directory

Move to the new directory using the cd command:

cd [directory-path]

For example, to move to the pnap directory we created in the previous step, run:

cd pnap

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:

touch .placeholder

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:

git status
Creating and staging a placeholder file in Git.

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"

For "message," specify a description for the commit. For example:

git commit -m "Created directory hierarchy"
Creating a 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]

For [remote-name], specify the name of the remote repository. For example:

git push origin
Pushing an empty directory to GitHub.

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.

Was this article helpful?
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Use git submodule init
September 28, 2022

This tutorial shows you how to use the git submodule init command to initialize submodules in a Git repository. See the most common usage examples.
Read more
How to Use Git Stash
September 13, 2022

Git stash is a way of storing unfinished work locally, without having to commit the changes. This tutorial shows how to create and use Git stash with examples.
Read more
Git Tag: An Overview of the Basic Functions
September 6, 2022

See an overview of basic functions to perform with Git tags - create, delete, push, replace, or checkout tags.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more