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.