git rm
Beginner Git CLI Careful
Summary: git rm <file> deletes a tracked file from disk and stages the deletion. git rm --cached <file> removes it from tracking only, leaving the file where it is — the fix for "committed but should have been ignored".
Careful — the plain form deletes the file from your disk (recoverable from the last commit, but any uncommitted edits in it are lost). --cached touches nothing on disk.
What it does
Stages the removal of the path from the next commit; without --cached, also deletes the working-tree file. Earlier commits still contain the file, so history is not affected.
Why it exists
Deleting in the editor leaves Git with a "deleted, not staged" entry to stage separately; git rm does both. And --cached is the only clean way to untrack a file that stays on disk.
When to use it
- Removing a file or folder from the project.
- Untracking a generated file or a secret that was committed by mistake (
--cached, plus a.gitignoreentry, plus rotating the secret).
When not to use it
- On a file with uncommitted edits you want: Git refuses unless forced; commit or stash first.
- To delete an untracked file: just delete it (Git never knew it).
- To remove a file from past history: that needs lesson 8.7.
Syntax
| Form | Meaning |
|---|---|
git rm <file> |
Delete from disk and stage the deletion |
git rm --cached <file> |
Stop tracking; keep the file on disk |
git rm -r <folder> |
Recursively, for a folder |
git rm -f <file> |
Force, even with uncommitted edits (loses them) |
Examples
$ git rm docs/scratch.mdrm 'docs/scratch.md'$ git statusOn branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: docs/scratch.md
A file already deleted in the file manager:
$ git status -s D docs/tmp.md$ git rm docs/tmp.mdrm 'docs/tmp.md'$ git status -sD docs/tmp.md
Untrack without deleting (after adding todo.txt to .gitignore):
$ git rm --cached todo.txtrm 'todo.txt'$ git status -s M .gitignore
D todo.txt
ls todo.txt still finds the file; after the commit, Git ignores it.
Expected result
git status shows deleted: under "Changes to be committed" (D in the first column); the file is gone from disk unless --cached. Commit to record the removal.
Common mistakes
git rmon a modified file.error: the following file has local modifications. Decide: commit the edits first, or-fto discard them.- Expecting
--cachedto delete. It does not; that is its purpose. - Thinking the file is gone from history. Every earlier commit still has it:
git show HEAD~1:<path>prints it. For secrets, rotate them. - Forgetting the
.gitignoreentry after--cached: the file reappears as untracked and someone adds it again.
How to undo or recover
- Before committing:
git restore --staged <file>(unstage the deletion) thengit restore <file>(bring the file back from the last commit). - After committing:
git restore --source=HEAD~1 <file>and commit, orgit revertthe commit (lesson 12.5).
In VS Code
Delete in the Explorer, then stage the D entry in Source Control; Discard Changes on a deleted file restores it.
In IntelliJ IDEA
Delete in the Project view; IntelliJ asks whether to remove the file from Git as well.
Related commands
git mv renames · git restore brings a file back · git add stages a deletion made by hand · git clean deletes untracked files.