Horje
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"

Screenshot-2024-04-27-091017

3. Add the file to the Git staging area:

git add empty_folder/.gitkeep

Screenshot-2024-04-27-091457

4. Commit the changes:

git commit -m "Add empty folder placeholder"

Screenshot-2024-04-27-091609

5. Push the changes to the remote repository:

git push

Screenshot-2024-04-27-091705



Reffered: https://www.geeksforgeeks.org


Git

Related
How to Add Submodule in Git? How to Add Submodule in Git?
.gitattributes vs .gitignore in Git .gitattributes vs .gitignore in Git
How to Merge Two Branches in Git? How to Merge Two Branches in Git?
How to Delete Branch in Github? How to Delete Branch in Github?
How to Delete Local Branch in Git? How to Delete Local Branch in Git?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14