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).
Aliases the pros type
Memorize the git restore concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:
git config --global alias.st # → git restoreg restgit restoreRestore a file to last commit state.
g ihrsgit restore --stagedUnstage. Pro tip: some call it 'ihrs' after a famous config.
g rest --source=HEAD^git restore --source=HEAD^ fileReincarnate a file from the previous commit.
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.
Pro tip
Naming is the whole game: `git switch` changes branches, `git restore` changes files, and `git reset` changes the branch + index pointer. Three commands, no ambiguity — this modern trio replaces most checkout usage.