Skip to content
Latchkey

git restore: Usage, Options & Common CI Errors

git restore is the modern, focused way to discard changes or unstage files.

git restore splits the file-restoring half of the old git checkout into a clearer command.

What it does

git restore updates files in the working tree (and/or the index with --staged) from a source, defaulting to HEAD. It does not move branches.

Common usage

Terminal
git restore file.txt          # discard working-tree changes
git restore --staged file.txt # unstage, keep working changes
git restore .                 # discard all working-tree changes
git restore --source=HEAD~2 file.txt

Options

FlagWhat it does
--staged / -SRestore the index (unstage)
--worktree / -WRestore the working tree (default)
--source=<tree> / -sRestore content from a commit/tree
--ours / --theirsPick a side during a conflict

Common errors in CI

error: pathspec 'X' did not match any file(s) known to git - the path is not tracked or is misspelled. To both unstage and discard, run git restore --staged --worktree <file>. Restoring discards uncommitted work permanently, so it is intentionally destructive.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →