KKAIROS
Practical guideIllustrative4 min read

Git Restore: unstage or discard a file

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.

Published:

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.

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

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

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:

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):

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:

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):

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:

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.

Ad

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

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.

All full guides: All guides