git restore --source: Usage, Options & Common CI Errors
By Daniel Zoghalchali·Latchkey
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.
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.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
git restore --source: Usage, Options & Common CI Errors?
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 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.