Description of git rm command

The git rm command removes specific files or a group of files from a Git repository. The main function of git rm is removing tracked files from the index. The git rm can also be used for removing files from both the staging index and the working directory. But a file can not be removed only from the working directory. It should be mentioned that the git rm command does not remove branches.

git revert1

Usage of git rm command

<file>…​ Specifies which files to remove.
Overrides the security check for making sure that the files in the HEAD correspond to the content of the staging index and working directory.
Executes the git rm command but in fact it does nor remove the files. It only outputs which files it would have removed.
-r This is a shorthand for 'recursive'. The git rm removes a target directory and its whole content when working in recursive mode.
-- Makes a clear distinction between a list of file names and the arguments which are passed to git rm.
--cached Specifies that deletion should occur only on the staging index. Working directory files are left alone.
--ignore-unmatch Exits with a 0 status even if no files matched. It is a Unix level status code.
Hides the output of the git rm command. The git rm command usually outputs one line for each removed file.

Undoing git rm

The git rm command updates the staging index and the working directory. These modifications will not be persisted until a new commit is created. The modifications are added to the commit history which means that they can be undone with git commands. The following command is used to undo a git rm. It reverts the current staging index and working directory back to the HEAD commit.

git reset HEAD
git checkout .

Use git reflog for finding a reference before the execution of the git rm, in the case that git rm is run and a new commit is created which persists the removal.

Git rm vs rm

When an ordinary shell rm command is being executed on a file it is tracking, Git repository recognizes it and updates only the working directory for reflecting the removal. For adding the modifications to the staging index the git add command must be executed too.The git rm command updates both the staging index and the working directory with the removal.


Let’s consider the following example:

git rm folder/\*.txt

We use a wildcard file glob to remove all *.txt files. These files are children of the folder directory and its subdirectories. We put slashes before asterisk to prevent the shell from expanding the wildcard. Then the wildcard expands the pathnames of files and subdirectories.

git rm -f git-*.sh

The force option is used in the example to remove the target files from the working directory and staging index.

Removing files no longer existing in the filesystem

If you want to record all the removed files as a component of the following commit, execute git commit -a command, and all the removal events will be added to the staging index in preparation of the following commit.

But if you want to continuously remove all the files that have been removed with the shell rm, use the command below, which will create a list of all the removed files from the working directory and pipe it to git rm --cached for updating the staging index:

git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached

Practice Your Knowledge

What are the functions and options of the 'git rm' command?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?