Open In App

How to Add an Empty Directory to a Git Repository?

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Adding an empty directory to a Git repository can be a challenging task, as Git doesn’t track empty directories by default. However, it is essential to keep track of empty directories in a repository, especially if you are working in a team or if you want to maintain the directory structure of your project.

Fortunately, there are a few ways to add an empty directory to a Git repository, and in this article, we’ll be discussing two of the most commonly used methods.

Method 1: Using a placeholder file

The easiest way to add an empty directory to a Git repository is to create a placeholder file in the directory and add it to the repository. This file can be a simple text file, such as .gitkeep or .gitignore, and it acts as a marker to indicate that the directory should be tracked by Git. To do this, follow these steps:

Create a new directory in your local repository. You can do this using a file explorer or by running the following command in your terminal:

mkdir <directory_name>

Create a new file in the directory. For example, you can create a file named .gitkeep. You can do this using a text editor or by running the following command in your terminal:

cd <directory_name>

Add the new file to the Git repository by running the following command:

git add .gitkeep

Commit the changes to the repository with a commit message:

$ git commit -m “Adding empty directory with .gitkeep”

Method 2: Using a .gitignore file

Another way to add an empty directory to a Git repository is to create a .gitignore file in the directory. The .gitignore file is used to specify files or directories that should be ignored by Git. To add an empty directory, follow these steps:

Create a new directory in your local repository. Change your current directory to the newly created directory by running the following command:

$ cd <directory_name>

Create a new file in the directory with the name .gitignore. You can do this using a text editor or by running the following command in your terminal:

$ touch .gitignore

Add the following line to the .gitignore file:

# Ignore everything in this directory
*
# Except this .gitignore file
!.gitignore

Add the .gitignore file to the Git repository by running the following command:

$ git add .gitignore

Commit the changes to the repository with a commit message:

git commit -m “Adding empty directory with .gitignore”

In conclusion, adding an empty directory to a Git repository can be done using either a placeholder file or a .gitignore file. Both methods work well and allow you to keep track of the empty directory in your repository. The method you choose to use will depend on your personal preference and the requirements of your project.

If you are working in a team, it’s recommended to have a consistent approach to adding empty directories to the repository, so everyone is on the same page. 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads