Undo a git rm and get the file back
I ran git rm and deleted a file — how do I restore it?
Short answer
If you haven't committed the deletion, git restore --staged --worktree <file> brings the file back from HEAD. If you already committed, restore it from the previous commit with git restore --source=HEAD~1.
git restore --staged --worktree path/to/file
Undoes an uncommitted git rm, restoring both the index and the file
Does this match your situation?
- You ran git rm and want the file back.
- git rm -r removed a whole directory you needed.
- You committed the deletion and now need to reverse it.
Step-by-step fix
Not committed yet? Restore from HEAD
git rm stages a deletion and removes the file from disk. Restoring both the index entry and the working file undoes both halves in one command.
step 1git restore --staged --worktree path/to/file # classic equivalent: git reset HEAD path/to/file git checkout -- path/to/fileUndo a whole directory removal
The same command works on a directory, or on everything at once if the removal was broad.
step 2git restore --staged --worktree src/ # undo the entire staged removal: git restore --staged --worktree .Already committed the deletion?
The file still exists in the parent commit, so pull it back from there. This creates a normal modification you can commit.
step 3git restore --source=HEAD~1 -- path/to/file git add path/to/file git commit -m "Restore accidentally deleted file"Don't know which commit removed it?
Search all of history for the deleting commit, then restore from its parent using the caret.
step 4git log --all --full-history --diff-filter=D -- "**/YourFile.cs" git restore --source=a1b2c3d^ -- src/YourFile.csIf you used --cached, the file is still on disk
git rm --cached only untracks — your local copy was never deleted. Re-add it if untracking was the mistake.
step 5git add path/to/file
Why this works
git rm does two separate things: it stages a deletion in the index and it removes the file from your working directory. The content itself is untouched in the object database, because it's part of every commit that already contained it. So 'undoing' the removal is simply asking Git to write that content back — from HEAD if you haven't committed, or from the parent commit if you have. Nothing is ever recovered from thin air; it was always still there.
How to stop it happening again
- Run git rm -n (dry run) first when removing more than one file.
- Use --cached when you only want to untrack, not delete.
- Commit before any bulk deletion so a single reset undoes it.
Commands used in this guide
git rmDelete a file from the repository - or just stop tracking it.
git restoreThrow away working-directory changes or unstage files (the modern, clearer replacement for checkout).
git checkoutThe old all-in-one: switch branches, restore files, or detach HEAD.
git logBrowse commit history with as much or as little detail as you want.
Still stuck?
Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.
Browse all guides →Frequently asked questions
How do I undo git rm before committing?
git restore --staged --worktree <file>. That reverses both halves of what git rm did: it clears the staged deletion and writes the file back to disk from HEAD.
What if I already committed the deletion?
Restore it from the previous commit: git restore --source=HEAD~1 -- path/to/file, then add and commit. If the deletion was several commits ago, find it with git log --diff-filter=D and restore from that commit's parent.
What's the difference between git rm and git rm --cached?
git rm deletes the file from disk and stages the deletion. git rm --cached only removes it from Git's index, leaving your local copy in place — that's the command for making .gitignore apply to an already-tracked file.
Related rescue guides
How do I revert just one file to a previous version?
git restore --source=<commit> -- <path> rewrites one file to its state at that commit and leaves everything else alone. The classic equivalent is git checkout <commit> -- <path>. Neither moves your branch or affects any other file.
Read the fix →Always fixableA file was deleted months ago — how do I find and restore it?
git log --all --full-history -- "**/filename" finds every commit that ever touched the file, including the one that deleted it. Restore it from the commit before the deletion with git checkout <hash>^ -- <path>.
Read the fix →Always fixableWhy is .gitignore not working for files that are already tracked?
.gitignore only applies to untracked files. Once a file has been committed, Git keeps tracking it no matter what the ignore rules say. Remove it from the index with git rm --cached <file> — that stops tracking while leaving the file on your disk.
Read the fix →Rarely recoverable by GitHow do I throw away all my local changes in Git?
git restore . discards edits to tracked files and git clean -fd deletes untracked ones. Both are irreversible for anything that was never staged or committed, so run git stash first if there's any doubt — it takes a second and makes the whole operation undoable.
Read the fix →