git restore
Restore files in the working tree or the staging area
The 'git restore' command puts files back to a known version. By default it overwrites a working file with its staged version, discarding your edits. With --staged it takes a file out of the staging area and keeps your edits on disk. With --source you can bring back a file as it was in any commit. It was added in Git 2.23 to take over the file-restoring half of git checkout.
Synopsis
git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...Options
Choosing the Right Option
restore vs older commands
The same actions, old and new
| Command | Older equivalent | Effect |
|---|---|---|
git restore app.js | git checkout -- app.js | Discard unstaged edits |
git restore --staged app.js | git reset app.js | Unstage, keep edits |
git restore --source=HEAD~1 app.js | git checkout HEAD~1 -- app.js | File from an old commit |
Examples
Discard edits
git restore app.jsThrow away the changes in app.js that are not staged.
Unstage a file
git restore --staged app.jsTake app.js out of the staging area, keep the edits.
File from an old commit
git restore --source=HEAD~2 app.jsGet app.js as it was two commits ago.
Workflow Examples
git add .Everything is staged
git restore --staged README.mdREADME.md is unstaged
git commit -m "Add to cart"The commit leaves README.md out
Troubleshooting
error: pathspec 'x' did not match any file(s) known to git
Cause: The file is untracked or the path is wrong
Check the path with git status; restore only works on tracked files
I restored a file and lost my edits
Cause: git restore overwrites the working file
Unsaved edits are gone; your editor undo history may still have them. Next time, git stash first
Tips & Pitfalls
Best Practices
- ✓Run git diff before git restore to see what you are about to throw away.
- ✓Use git restore --staged to unstage — it never touches your edits.
- ✓Prefer git restore over git checkout for files: its name says what it does.
Common Mistakes
- ✗Running git restore <file> on edits you wanted to keep — discarded edits cannot be recovered.
- ✗Expecting git restore --staged to discard your edit: it only unstages it.
- ✗Using git restore to undo a commit — it restores files; use git reset or git revert for commits.
FAQ
git restore changes files. git reset moves the branch to another commit (and can reset files too).
Not for discarded working edits: Git never stored them. Unstaging with --staged loses nothing.
Git 2.23 or newer. On older versions use git checkout -- <file> and git reset <file>.