Skip to content
Latchkey

git restore --source: Usage, Options & Common CI Errors

git restore --source brings a file back from any commit without changing branches.

When you need one file as it existed at an earlier revision - not a full checkout - restore --source is the focused tool. It is the modern replacement for git checkout <rev> -- <path>.

What it does

git restore --source=<tree> copies the named paths from that commit or tree into the working tree (and the index too if --staged is added), without moving HEAD or any branch.

Common usage

Terminal
git restore --source=HEAD~2 file.txt
git restore -s main -- path/to/file
git restore --source=v1.0.0 --staged --worktree config.yml
git restore -s origin/main -- src/

Options

FlagWhat it does
--source=<tree> / -sCommit/tree to restore content from
--staged / -SAlso restore into the index
--worktree / -WRestore the working tree (default)
-- <path>Limit to specific paths

Common errors in CI

error: pathspec ‘X’ did not match any file(s) known to git - the path does not exist in that source revision, or the revision is absent on a shallow clone. Verify the path existed then (git ls-tree <rev>), and deepen history if the source commit was never fetched.

Related guides

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