Signup/Sign In

Git Rm Command

Git Rm is a command that can be used to remove files from the staging area and the working directory. Rm stands for remove and is similar to the Linux command rm which is used to delete files. The Git Rm command is mainly used to unstage files. Let’s learn more about the Git Rm command.

What is Git Rm?

  • As discussed above, Git Rm is a command to remove files from our Git repository.

  • This can also be done by manually searching for the file in the Repository and deleting it and then staging the changes but the Git Rm command does all this in a single step.

  • The Linux command rm can also be used but we would again have to stage those changes.

  • To make sure that our repository reflects the removal of files we need to commit these changes.

  • We can remove a file from the staging as well as from our file system by using this command. However, just removing a file from the working directory is not possible i.e. it is not possible to remove an untracked file.

Git Rm Command

To remove a file from the staging area as well as the working directory we can just pass the file name to the Git Rm command. We can also use Glob Patterns to specify certain files.

$ git rm <file-name> 

To remove multiple files at once:

$ git rm <file-1> <file-2> <file-3>

We can also remove files from just the staging area by using the --cached option.

$ git rm --cached <file-name>

A lot of times Git will block the removal of files that have unsaved changes in them. This is a safety feature used by Git to make sure that we are not removing a file that we made changes to since the last commit. We can use the -f or --force to forcefully remove this file.

$ git rm -f <file-name>

There is an option to dry-run a Git Rm command. This won't remove any files and can be useful in situations where you just want to see the files that will be removed by the Git Rm command. This is mostly applied when we have used glob patterns to specify certain files or delete multiple files in our directory.

$ git rm --dry-run <glob-pattern>

How to Reverse a Removal?

There may be cases when we remove a file and then again need the same file back. We can undo the effect of the Git Rm command as long as we have not committed the changes by removing the file.

We can use the following commands to nullify the effects of a Git Rm.

Git reset command can be used to go back to the last commit(HEAD).

$ git reset HEAD

We can also use the Git Checkout command to go back to the previous commit

$ git checkout .

However, there are cases where we cannot undo a Git Rm command. Suppose we just initialized a Git Repository and created a file and staged it. Now if we run the Git Rm command to remove this file we will get an error as Git will block this since there are changes in the file that have been staged. We can bypass this using the -f flag and remove this file. Now there is no way to go back to any commit as no commit exists and we have lost our file.

Summary

A lot of times we will find ourselves in a situation where we have a lot of unnecessary files in our repository. Git Rm is a command that helps us to remove such files. In most cases, we will be using it to remove files from the staging area. We can delete files from both the staging area and working directory but this command cannot remove untracked files. We can also get our file back if no commit has been made after removing it. However, in the rare case where we don’t have even an initial commit in our repository, we won’t be able to get our files back.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight