How Do I Add an Empty Directory to a Git Repository?

Better Stack Team
Updated on June 24, 2024

Git does not track empty directories by design. However, you can add a placeholder file within the directory to make Git recognize it. Here's how you can do it:

Step 1: Create the Empty Directory

Create the empty directory within your Git repository:

 
mkdir directory_name

Replace directory_name with the name of your empty directory.

Step 2: Add a Placeholder File

Create a placeholder file within the empty directory. Conventionally, this file is named .gitkeep or .keep, but you can use any name:

 
touch directory_name/.gitkeep

Step 3: Add and Commit

Add the new file to the staging area and commit it to the repository:

 
git add directory_name/.gitkeep
git commit -m "Added empty directory"

Note:

  • The .gitkeep file serves as a convention to indicate that the directory should be kept even if it's empty. Git will recognize the directory with this placeholder file.
  • Alternatively, you can use any other file within the empty directory. Just make sure it's not something that will be ignored by your .gitignore file.
  • Be cautious with adding unnecessary empty directories to your repository, as it may clutter the repository structure. Only add them if they serve a specific purpose or organization within your project.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github