How to Add Empty Folder in Git?

Maintaining directory structure in a Git repository often requires the addition of empty folders. While Git doesn’t track empty folders directly, there are several approaches to accomplish this. In this article, we’ll explore one of the most commonly used approaches, which involves using a placeholder file.

Approach to Add Empty Folder in Git

We will be using a Placeholder File. This approach involves creating an empty file inside the folder and committing it to the repository as a placeholder. By adding this placeholder file, we signal Git to keep the empty folder in the repository.

Syntax:

touch <folder_path>/.gitkeep
git add <folder_path>/.gitkeep
git commit -m "Add empty folder placeholder"
git push

Example:

Let’s go through an example to illustrate how to add an empty folder named empty_folder to our Git repository.

1. Navigate to the directory where you want to add the empty folder:

cd path/to/repository

2. Create an empty file inside the folder:

touch empty_folder/.gitkeep

or

New-Item -ItemType directory -Path "empty_folder"
New-Item -ItemType file -Path "empty_folder\.gitkeep"

3. Add the file to the Git staging area:

git add empty_folder/.gitkeep

4. Commit the changes:

git commit -m "Add empty folder placeholder"

5. Push the changes to the remote repository:

git push


Contact Us