Skip to content
LatchkeyLatchkey home

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.

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: Usage, Options & Common CI Errors?
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 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

References

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