git restore
Undo changes in your working tree or index — surgically.
git restoreWhat it does
git restore brings back file contents from somewhere else: from the index, from HEAD, or any commit. It's the file-scoped half of the old checkout, with zero branch-switching ambiguity. Discard edits, or unstage — cleanly, by path.
Working Tree
files on disk
Index · Staging
.git/index
Repository
objects/ → commits
committed history
restore copies file blobs back from the index or HEAD into the working tree
Watch it — press play
A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.
Step 1
Messed up a file
Your working copy of src/bug.js is broken. Restore rewrites it from the index (or HEAD).
git restore src/bug.jsWorking Tree
files on disk
Index · Staging
.git/index
Repository
objects/ → commits
committed history
The index copy is healthy — let's take it back
Under the hood
Two directions, one command
`git restore <file>` writes the file from the index over the working copy — discarding your edits. `git restore --staged <file>` writes HEAD's version into the index — unstaging it. Either way it copies a blob to a destination.
Like checkout --, but explicit
Restore exists because `git checkout <path>` was the source of countless grand mysteries. Restore separates 'change what I'm on' (switch) from 'change these files' (restore).
Any source is fair game
`git restore --source=HEAD~2 file` resurrects the file as it was two commits ago. Restore reads that blob and writes it to the worktree (or index with --staged).
Command anatomy
--stagedRestore the INDEX version — i.e. unstage the path.
--source=<rev>Restore from an arbitrary commit instead of HEAD.
--worktreeRestore only the working-tree copy (default interaction).
When to reach for it
You mangled a file and want HEAD's version back, instantly.
You staged a file by accident — restore --staged to unstage just it.
Pulling a single old version of a file out of history with --source.
Practice
A real repo, pre-seeded with history, runs right here — stage, commit, branch, stash, diff, even hit and resolve a real merge conflict. Nothing to install, nothing leaves your machine.
Open practice playground