- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git restore Tutorial
Explains how git restore recovers file state in the working tree or index, and how it differs from reset and checkout.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
The short version
git restore is for restoring file content to a known state. It focuses on paths, not on moving branch pointers.
Why restore exists
Older Git workflows used git checkout -- <path> for many file-recovery tasks. Git later introduced restore so that “switch branches” and “restore file content” would be expressed by different commands.
Two main jobs
Restore the working tree
git restore README.md
This discards unstaged working tree changes and restores the file from the index.
Unstage a file
git restore --staged README.md
This resets the staged version back to HEAD, which makes it a clear way to undo git add.
Restore both index and working tree
git restore --staged --worktree README.md
Use this when you want to unstage and discard the local working copy together.
Restore from a specific commit
git restore --source=HEAD~1 src/app.ts
That lets you recover one path from another commit without moving the entire branch.
restore vs reset
restorefocuses on file stateresetcan affect the index and may also move HEAD
If your real question is “How do I get this file back?”, start with restore.
What problem this command solves in a workflow
git restore is dedicated to recovering file content to a known state. It focuses on paths, not on moving branch pointers. The command can restore the working tree from the index, unstage files back to HEAD, or pull content from any specific commit — making it the clearest tool for "how do I get this file back?" questions.
Typical use cases
- Discard unstaged working tree changes with
git restore <path>, reverting the file to match the index. - Undo
git addwithgit restore --staged <path>, resetting the staged version back to HEAD without touching the working tree. - Recover a file from a specific commit using
git restore --source=<commit> <path>, without moving the entire branch.
Diagram view
Special cases and boundaries
git restore <path>discards unstaged changes in the working tree; the overwritten content is not recoverable through Git.--stagedresets the index to match HEAD, effectively undoinggit addwithout touching working tree files.- Using both
--stagedand--worktreetogether restores the file completely to the source commit's version. - The
--sourceflag lets you recover content from any commit, tag, or ref, not just HEAD — useful for selective file recovery from history.