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

> git restore --source pulls file contents from a specific commit into the working tree. Reference for -s, combining with --staged, and the pathspec errors in CI.

Source: https://latchkey.dev/learn/command-reference/git-restore-source  
Updated: 2026-06-25

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

| Flag | What it does |
| --- | --- |
| --source=<tree> / -s | Commit/tree to restore content from |
| --staged / -S | Also restore into the index |
| --worktree / -W | Restore 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
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
