Git

How to Remove Directory in Git

Git is the most used decentralized version control system all over the globe to make version records of each folder and file. Each change can be examined through a Git local repository as well as a remote repository. Deleting the Git directory from the Git remote repository is quite similar to deleting a normal bash folder and file. Don’t know how to do that? No worries!

In this guide, we have explained the method of removing a directory in Git.

How to Remove Directory in Git?

When users have multiple directories or folders in the Git repository, sometimes, they want to delete an unnecessary directory. For the corresponding purpose, try out the below-listed steps.

Step 1: Create Repository

Open up GitHub hosting service and press the “+” icon to create a new repository:

Next, specify the repository name, mark the “Public” option to permit everyone to access it, and click on the “Create repository” button:

Step 3: Launch Git Bash

To launch the “Git Bash” on your system, press the “CTRL + Esc” key to open the “Startup” menu and search it:

Step 4: Navigate to Git Directory

Move to the specific Git directory with the help of the provided command:

$ cd "C:\Users\nazma\Git"

Step 5: Copy HTTPS URL

Open the selected remote repository that you want to clone, hit the “Code” button, and copy the “HTTP URL” to the clipboard. Here, we want to clone the “mari_khan” remote repository:

Step 6: Clone Remote Repository

Run the below-given command with copied HTTP URL to clone the repository:

$ git clone https://github.com/GitUser0422/mari_khan.git

As you can see, our remote repository “mari_khan” is cloned successfully:

Step 7: Navigate to Git Repository

Next, move to the cloned repository using “cd” command:

$ cd "C:\Users\nazma\Git\mari_khan"

Step 8: Create Git Directory

Next, run the “mkdir” to create a new directory under the cloned repository:

$ mkdir My_dir

Move to the “My_dir” directory by utilizing the given command:

$ cd "My_dir"

Step 9: Create and Update File

Execute the “echo” command to create and update a file named “File1.txt” in the Git directory:

$ echo "First file"> File1.txt

Step 10: Add File into Git Directory

Add the created file into the Git directory:

$ git add File1.txt

Step 11: Navigate to Git Repository

Next, navigate back to the Git repository:

$ cd -

Step 12: Check Status

Now, check the Git repository status:

$ git status

Step 13: Check Git Repository List

Check the list of Git repositories:

$ ls My_dir

Below output indicates that we have “File1.txt” file in Git repository:

Step 14: Remove Git Directory

Remove the Git directory using the “rm” command with “-r” flag:

$ rm -r My_dir

Here, the “-r” option will remove the “My_dir” directory recursively:

Step 15: Check Status

Again, check the Git status:

$ git status

Below output shows that our specified Git directory is deleted successfully:

We have provided the process of removing the directory in Git.

Conclusion

To remove the directory in Git, first, create a new remote repository, open up the “Git Bash” terminal, and navigate to the directory. Copy the “HTTP URL” and paste it to the terminal to clone it to the Git repository. Next, create and move to the Git directory. After that, create and update the file using and execute the “$ rm -r” command to remove the directory. In this guide, we have explained the method of removing a directory in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.