# Git Restore: unstage or discard a file

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-restore-full-en
`git restore FILE` replaces the working-tree file with the version in the index, so it discards edits that are not staged. `git restore --staged FILE` puts the index entry back to the HEAD version, so the change is no longer staged, and leaves the working-tree file as it is.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-70-A, TE-70-B and TE-70-C. The expected non-zero exit is TE-70-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-70-A: restore --staged keeps the edit, plain restore discards it

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txt; git add f.txt
```

Command:

```bash
git restore --staged f.txt; echo staged=$(git diff --cached --name-only | wc -l); cat f.txt; git restore f.txt; echo after_restore:; cat f.txt
```

Recorded output:

```
staged=0
one
two
after_restore:
one
```

Exit status: 0. The output matches the expected text written before the run.

### TE-70-B: plain restore takes the version from the index (two), not from the last commit (one)

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git add f.txt
printf 'three\n' > f.txt
```

Command:

```bash
git restore f.txt; cat f.txt; echo staged=$(git diff --cached --name-only)
```

Recorded output:

```
two
staged=f.txt
```

Exit status: 0. The output matches the expected text written before the run.

### TE-70-C: expected non-zero exit: git restore with a path Git does not know stops with an error and exit status 1

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
```

Command:

```bash
git restore nosuch.txt
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
error: pathspec 'nosuch.txt' did not match any file(s) known to git
```

Exit status: 1. This status and the output match what was written before the run.

## Reading the recorded output

TE-70-A starts with an edit that is staged. After git restore --staged, the count of staged files is 0, and cat still prints both lines, one and two, so the edit stayed in the working tree. After plain git restore the file prints only one, so the edit that was not staged is gone. TE-70-B separates the index from the last commit: by the setup, the last commit holds one, the index holds two and the working tree holds three. After git restore the file reads two, which is the index version, and f.txt is still listed as staged.

TE-70-C: by the setup, no file named nosuch.txt exists. git restore nosuch.txt exited with status 1 and printed error: pathspec 'nosuch.txt' did not match any file(s) known to git.

## Limits

Warning, not a test result: `git restore FILE` discards edits that are not committed or staged, and Git cannot bring them back. The runs use a throwaway directory. `--source` and restoring deleted files were not tested.

## Source

- https://git-scm.com/docs/git-restore

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-70-A` | restore --staged keeps the edit, plain restore discards it | 0 | PASS | `ab029bd161fc` |
| `TE-70-B` | plain restore takes the version from the index (two), not from the last commit (one) | 0 | PASS | `da655f577a3e` |
| `TE-70-C` | expected non-zero exit: git restore with a path Git does not know stops with an error and exit status 1 | 1 | PASS | `0e6afd91a77e` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Switch: change branches](https://kairos-2z6s.onrender.com/res/git-switch-full-en)
- [Git Show: read one commit](https://kairos-2z6s.onrender.com/res/git-show-full-en)
- [Git Rev Parse: ask Git for a path or a commit id](https://kairos-2z6s.onrender.com/res/git-rev-parse-full-en)
- [git reset: soft, mixed and hard on the same repository](https://kairos-2z6s.onrender.com/res/git-reset-modes-full-en)
- [git tag: name a commit, lightweight and annotated](https://kairos-2z6s.onrender.com/res/git-tag-full-en)
- [git revert: undo a commit by adding a new one](https://kairos-2z6s.onrender.com/res/git-revert-full-en)
