Skip to content
LatchkeyLatchkey home

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.

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@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git 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.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card