How to delete folder in GitHub repository and local git repository

Sometimes we want to delete folders from GitHub but there is no option in
GitHub to delete folder directly. If you want to delete folder from
GitHub then we need to open the folder and delete all files one by one and then the folder will be automatically disappeared / deleted.

In git repository, the folder should not exist without even one file. If no file exist in the folder then automatically the folder will be deleted.

Even if you want create folder, then we should create/add one file in that folder.

Create New Folder in GitHub

Create folder in Github
Create folder in Github
New Folder (Folder1) created in Github
New Folder (Folder1) created in Github

But here if you have N..number of files in the folder to delete then it is tedious process. To overcome this tedious process, we can delete that folder from git bash from local git repository.

Create New Folder from GitBash

Create new folder with name Folder1 and then add file like sample.txt and then add to stage, commit and then push to remote repository.

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial (master)
$ mkdir Folder1

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial (master)
$ cd Folder1/

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
$ touch sample.txt

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ./

nothing added to commit but untracked files present (use "git add" to track)

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
$ git add .

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
$ git commit -m "new folder creted"
[master c2d57a0] new folder creted
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Folder1/sample.txt

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 331 bytes | 110.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/NarayanaTutorial/NarayanaTutorial.git
   73535af..c2d57a0  master -> master

[email protected] MINGW64 /d/narayanatutorial/NarayanaTutorial/NarayanaTutorial/Folder1 (master)
New Folder (Folder1) created from GitBash
New Folder (Folder1) created from GitBash

Delete New Folder from Github Console

Open the folder(Folder1) in github then go to list of files and then delete one by one by clicking on delete icon highlighted in the below image. When last file delete then the folder (Folder1) will be deleted automatically.

Delete Folder from Github
Delete Folder (Folder1) from Github

After deleting the file, folder (Folder1) deleted automatically.

Folder (Folder1) deleted from Github
Folder (Folder1) deleted from Github

Delete folder from GitBash

First up all you can clone or pull the git repository. if you are already working on the same remote repository then you can use full else clone the project into local system

Here I am executing git pull command to sync my local repository.

Step 1

git pull

git pull command
git pull command

Step 2

git rm -rf Folder1

Step 3

git rm -rf Folder1

Step 4

git add .

Step 5

git commit -m “Folder1 deleted”

Step 6

git push

Delete folder form GitBash
Delete folder form GitBash

You can see the final output from Github

Github Console - Folder deleted from GitBash
Github Console – Folder deleted from GitBash


The Author

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply