KKAIROS
Practical guideIllustrative5 min read

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.

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

Command:

git reset --mixed HEAD -- f.txt; echo EXIT=$?; echo '--cached:'; git diff --cached --name-only; echo '--worktree:'; git diff --name-only

Recorded output:

Unstaged changes after reset:
M	f.txt
EXIT=0
--cached:
--worktree:
f.txt

Standard 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.txt

Command:

git reset --hard HEAD >/dev/null; cat f.txt

Recorded output:

one

TE-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 two

Command:

git reset --soft HEAD~1 >/dev/null; echo 'cached:'; git diff --cached --name-only; echo 'head:'; git log --oneline | wc -l

Recorded output:

cached:
f.txt
head:
1

TE-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/null

Command:

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

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

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

Command:

git reset --hard nosuchref; rc=$?; cat f.txt; exit $rc

Recorded output:

edit

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

Ad

Source

Evidence

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