git reset: soft, mixed and hard on the same repository
reset moves the branch pointer to a target commit (default HEAD). --soft moves only the pointer; --mixed also resets the index; --hard also resets tracked working-tree files.
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-05-A: git reset --mixed restores the index to the target commit and keeps working-tree changes
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.txtCommand:
git reset --mixed HEAD -- f.txt; echo EXIT=$?; echo '--cached:'; git diff --cached --name-only; echo '--worktree:'; git diff --name-onlyRecorded output:
Unstaged changes after reset:
M f.txt
EXIT=0
--cached:
--worktree:
f.txtStandard error from the same run:
warning: --mixed with paths is deprecated; use 'git reset -- <paths>' instead.This run checks the exit status and records the output; it asserts no expected text.
TE-05-B: git reset --hard resets tracked files in the working tree and discards uncommitted edits
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.txtCommand:
git reset --hard HEAD >/dev/null; cat f.txtRecorded output:
oneTE-05-C: git reset --soft moves the branch pointer and leaves index and working tree alone
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; git commit -qm twoCommand:
git reset --soft HEAD~1 >/dev/null; echo 'cached:'; git diff --cached --name-only; echo 'head:'; git log --oneline | wc -lRecorded output:
cached:
f.txt
head:
1TE-05-D: the reset target need not be an earlier commit: reset can move the branch forward
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 c1
printf 'two\n' >> f.txt; git add f.txt; git commit -qm c2
printf 'three\n' >> f.txt; git add f.txt; git commit -qm c3
git rev-parse HEAD > .c3
git reset --hard HEAD~2 >/dev/nullCommand:
git reset --soft $(cat .c3); echo move=$?; echo head=$(git log --oneline -1 --format=%s); echo staged=$(git diff --cached --name-only | tr '\n' ' ')Recorded output:
move=0
head=c3
staged=f.txtTE-05-E: expected non-zero exit: git reset --hard with a name that is not a commit stops with a fatal error and changes nothing
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
printf 'edit\n' > f.txtCommand:
git reset --hard nosuchref; rc=$?; cat f.txt; exit $rcRecorded output:
editStandard error from the same run:
fatal: ambiguous argument 'nosuchref': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'Exit status: 128. This status and the output match what was written before the run.
TE-05-E: git reset --hard nosuchref exited with status 128 and printed fatal: ambiguous argument 'nosuchref': unknown revision or path not in the working tree. to standard error. The cat after it printed edit, so the uncommitted edit was still in f.txt after the failed command.
Limits
Uncommitted edits discarded by --hard are normally unrecoverable through Git; it is not a general untracked-file deletion tool.
Source
Evidence
TE-05-A: git reset --mixed restores the index to the target commit and keeps working-tree changesTE-05-B: git reset --hard resets tracked files in the working tree and discards uncommitted editsTE-05-C: git reset --soft moves the branch pointer and leaves index and working tree aloneTE-05-D: the reset target need not be an earlier commit: reset can move the branch forward
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-05-A | git reset --mixed restores the index to the target commit and keeps working-tree changes | 0 | PASS | 0c537cd1cd69 | | TE-05-B | git reset --hard resets tracked files in the working tree and discards uncommitted edits | 0 | PASS | 00099b3ae2cc | | TE-05-C | git reset --soft moves the branch pointer and leaves index and working tree alone | 0 | PASS | f60ec3a500d5 | | TE-05-D | the reset target need not be an earlier commit: reset can move the branch forward | 0 | PASS | 8865a3f1a0c2 | | TE-05-E | expected non-zero exit: git reset --hard with a name that is not a commit stops with a fatal error and changes nothing | 128 | PASS | a317cb5829cf |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides