Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git rm command

Reference for git rm: delete tracked files, keep them with --cached, remove directories with -r, use --dry-run, and understand the safety checks and -f.

git rm removes files from the index and, by default, from your working directory too, so the deletion is staged for the next commit. It is the one-step equivalent of deleting the file yourself and then running git add on it.

The command has safety checks: it refuses to remove a file whose staged or working content differs from the last commit, because that would throw away work that exists nowhere else. Use --cached to stop tracking a file but keep it on disk, which is how you remove something that should have been ignored.

Terminal
git rm [-f] [-n] [-r] [--cached] [--] <pathspec>...

Try it

Bash Remove, keep with --cached, and the safety check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Staged changes, globbing and recovering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git rm <pathspec>...
git rm --cached <pathspec>...
git rm -r <directory>
Option Description
<pathspec>... Files or patterns to remove. Quote globs to let Git match them, including across directories.
-r Allow recursive removal when a directory is named.
--cached Remove from the index only; the working copy stays and becomes untracked.
-f, --force Override the up-to-date check.
-n, --dry-run List what would be removed without doing it.
-q, --quiet Suppress the rm 'file' lines.
--ignore-unmatch Exit 0 even if no file matched.
--sparse Allow removing entries outside the sparse-checkout definition.
--pathspec-from-file=<file>, --pathspec-file-nul Read the pathspecs from a file.

Errors: a path that does not match any tracked file produces fatal: pathspec 'x' did not match any files. A file with local modifications is refused with has local modifications and the hint to use --cached or -f; a file with changes staged that differ from HEAD says has changes staged in the index. Naming a directory without -r fails with not removing 'dir' recursively without -r.

Usage notes

  • Which check applies. Without options the file must match HEAD, the index and the working tree. --cached loosens this: it is allowed when the index matches either HEAD or the working file. -f skips the checks entirely and can discard unsaved work, so be sure.
  • Staying out of the repository. To stop tracking a file you want to keep (a config file, a build directory), run git rm --cached and add it to .gitignore in the same commit. Teammates who pull will lose their copy of the file, because the deletion is applied to their working trees.
  • The history keeps it. Removing a file does not delete it from previous commits. To purge a leaked secret from history you need git filter-branch or similar tools; rotate the secret anyway.
  • Not the same as the shell. rm file leaves the deletion unstaged. git add -u or git commit -a also stages it, but git rm is explicit and works for files already deleted by hand.
  • Empty parent directories are removed together with the last file in them.
  • Moving instead? Use git mv. Git records no rename: it detects renames later by comparing content.
  • Submodules. git rm of a submodule removes its working tree and the gitlink, and edits .gitmodules; the repository data stays in .git/modules.
  • Undo. Before committing: git restore --staged --worktree <path> (or git reset HEAD <path>; git checkout -- <path> on old versions). After committing: restore it from HEAD~ with git restore --source=HEAD~ -- <path>.