Undo a changeAlways fixable

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.

quick fix
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

  1. 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 1
    git restore --staged --worktree path/to/file
    
    # classic equivalent:
    git reset HEAD path/to/file
    git checkout -- path/to/file
    
  2. Undo a whole directory removal

    The same command works on a directory, or on everything at once if the removal was broad.

    step 2
    git restore --staged --worktree src/
    
    # undo the entire staged removal:
    git restore --staged --worktree .
    
  3. 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 3
    git restore --source=HEAD~1 -- path/to/file
    git add path/to/file
    git commit -m "Restore accidentally deleted file"
    
  4. Don't know which commit removed it?

    Search all of history for the deleting commit, then restore from its parent using the caret.

    step 4
    git log --all --full-history --diff-filter=D -- "**/YourFile.cs"
    git restore --source=a1b2c3d^ -- src/YourFile.cs
    
  5. If 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 5
    git 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 rm

Delete a file from the repository - or just stop tracking it.

git restore

Throw away working-directory changes or unstage files (the modern, clearer replacement for checkout).

git checkout

The old all-in-one: switch branches, restore files, or detach HEAD.

git log

Browse 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

← All Git Rescue guides