Git Tutorial 12 – Git Rm – Remove Files From Working And Staging (Index) Directory


As a part of GIT Tutorials End To End, we will learn about another frequently used and important git command “git rm“ in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

What is Git Rm command?

“Rm” stands for remove and this command is used to remove/delete files from your working directory and staging or git repository. Let’s learn about this command with examples

Removing untracked file

I have initiated a git directory and added a file as below:-

I have not added a new file “FileA” into staging/index using “git add” command yet. If we want to remove this file then we are NOT supposed to use “git rm” command as the file is yet to be tracked by git. If we try to remove this file using “git rm” command we will get an error as below.

To remove files not tracked by git, we can use “rm <fileName>” command as shown below:-

Removing staged file

Let’s create a new file again and add it to the staging area and try to delete the file using “git rm <fileName>” command. You will get an error as below:-

The reason behind this is that we have already staged/indexed FileA and have not committed yet. So here we have two options. Either use the “-f” option to forcefully remove the file from the index and working directory or use the “–cached” option to remove the file from the index/staging area but keep it local. In the second option, you will reach the state before the git add command. If you have committed the file then you will be able to remove the file easily.

git rm –cached <fileName>
git rm -f <fileName>

You can remove a committed file easily but need to recommit and push to make changes in a git repository.

Remove a directory

To remove the directory we need to use -r option with the command.

git rm -r <directoryName>

Please subscribe to my YouTube channel Retarget Common to learn from my video tutorials.

Below are important end to end tutorials for Testers:-

Selenium Tutorials

Rest Assured Tutorials

Postman Tutorials

TestNG Tutorials

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

1 thought on “Git Tutorial 12 – Git Rm – Remove Files From Working And Staging (Index) Directory

Leave a Reply

Your email address will not be published. Required fields are marked *