W3docs

git rm

On this page, you can find useful information about git rm command, learn how to use and undo it and see the difference of the shell rm and git rm command.

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. Note: git rm only works on tracked files. It will fail if you try to remove an untracked file.

git revert1

Usage of git rm command

<file> …​Specifies which files to remove.
-f --forceOverrides the security check for making sure that the files in the HEAD correspond to the content of the staging index and working directory.
-n --dry-runExecutes the git rm command but in fact it does not remove the files. It only outputs which files it would have removed.
-rThis 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.
--cachedSpecifies that deletion should occur only on the staging index. Working directory files are left alone.
--ignore-unmatchExits with a 0 status even if no files matched. It is a Unix level status code.
-q --quietHides 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. They can be undone with Git commands. To unstage the removal (leaving the file deleted in the working directory), use: git reset HEAD

git reset HEAD

To fully restore the file to its latest version in both the staging index and the working directory, use the modern git restore command: git restore

git restore <file>

(Note: git checkout . is deprecated for restoring files.)

To remove a file from the staging index only (leaving it in the working directory), use the --cached flag: git rm --cached

git rm --cached <file>

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.

Examples

Let’s consider the following example:

git rm with wildcards

git rm folder/\*.txt

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

git rm -f

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, stage the deletions first with git add -u git rm git 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 rm --cached

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

Practice

Practice

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