KKAIROS
Practical guideIllustrative12 min read

git reset: soft, mixed and hard on the same repository

Move the branch pointer and decide what happens to your files: --soft keeps the index and the working tree, --mixed resets the index, and --hard overwrites tracked files.

Published:

Updated:

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.

Reset a branch to the remote

How do I reset my branch to match the remote and throw away my own commits and edits?

Run git fetch origin, then git reset --hard origin/main, using the name of the remote branch you want. The branch, the index and the tracked files all move to the fetched commit, and an untracked file stays where it is (TE-301-A).

Can I undo that reset?

Committed work, yes: the git-reset manual says ORIG_HEAD is set to the tip of the branch before the operation, so git reset --hard ORIG_HEAD brings the commit back (TE-301-B). An edit that was never committed does not come back.

Why did the reset go to an old commit?

origin/main is a remote-tracking branch, and it only changes when you fetch. Without git fetch, the reset used the old value (TE-301-C).

TE-301-A: git fetch then git reset --hard origin/main moves the branch to the remote commit and leaves an untracked file alone

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

git init -q --bare -b main remote.git
git clone -q remote.git work
cd work
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'base\n' > f.txt; git add f.txt; git commit -qm base
git push -q origin main
cd ..
git clone -q remote.git other
cd other
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'remote-change\n' >> f.txt; git commit -qam remote-change
git push -q origin main
cd ../work
printf 'my-commit\n' >> f.txt; git commit -qam my-commit
printf 'scratch\n' > untracked.txt
printf 'dirty\n' >> f.txt

Command:

cd work
git fetch -q origin
git reset --hard origin/main
echo head=$(git rev-parse --short HEAD) origin=$(git rev-parse --short origin/main)
git log --oneline --format=%s
cat f.txt
echo untracked=$(cat untracked.txt)

Recorded output:

HEAD is now at 4619c8f remote-change
head=4619c8f origin=4619c8f
remote-change
base
base
remote-change
untracked=scratch

Exit status: 0. This is the status recorded in the log for TE-301-A.

TE-301-B: git reset --hard ORIG_HEAD brings back the commit that the reset removed, but not the uncommitted edit

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

git init -q --bare -b main remote.git
git clone -q remote.git work
cd work
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'base\n' > f.txt; git add f.txt; git commit -qm base
git push -q origin main
cd ..
git clone -q remote.git other
cd other
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'remote-change\n' >> f.txt; git commit -qam remote-change
git push -q origin main
cd ../work
printf 'my-commit\n' >> f.txt; git commit -qam my-commit
printf 'scratch\n' > untracked.txt
printf 'dirty\n' >> f.txt

Command:

cd work
git fetch -q origin
git reset -q --hard origin/main
echo after_reset=$(git log --oneline --format=%s | tr '\n' ' ')
git reset -q --hard ORIG_HEAD
echo after_undo=$(git log --oneline --format=%s | tr '\n' ' ')
cat f.txt

Recorded output:

after_reset=remote-change base
after_undo=my-commit base
base
my-commit

Exit status: 0. This is the status recorded in the log for TE-301-B.

TE-301-C: without git fetch first, git reset --hard origin/main uses the old origin/main

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

git init -q --bare -b main remote.git
git clone -q remote.git work
cd work
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'base\n' > f.txt; git add f.txt; git commit -qm base
git push -q origin main
cd ..
git clone -q remote.git other
cd other
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'remote-change\n' >> f.txt; git commit -qam remote-change
git push -q origin main
cd ../work
printf 'my-commit\n' >> f.txt; git commit -qam my-commit
printf 'scratch\n' > untracked.txt
printf 'dirty\n' >> f.txt

Command:

cd work
git reset -q --hard origin/main
echo no_fetch_log=$(git log --oneline --format=%s | tr '\n' ' ')
git fetch -q origin
echo origin_after_fetch=$(git log origin/main --oneline --format=%s | tr '\n' ' ')

Recorded output:

no_fetch_log=base
origin_after_fetch=remote-change base

Exit status: 0. This is the status recorded in the log for TE-301-C.

TE-301-D: expected non-zero exit: git reset --hard to a remote branch that does not exist stops with status 128 and keeps the commit and the edit

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

git init -q --bare -b main remote.git
git clone -q remote.git work
cd work
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'base\n' > f.txt; git add f.txt; git commit -qm base
git push -q origin main
cd ..
git clone -q remote.git other
cd other
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'remote-change\n' >> f.txt; git commit -qam remote-change
git push -q origin main
cd ../work
printf 'my-commit\n' >> f.txt; git commit -qam my-commit
printf 'scratch\n' > untracked.txt
printf 'dirty\n' >> f.txt

Command:

cd work
git fetch -q origin
git reset --hard origin/nosuchbranch; rc=$?
cat f.txt | tr '\n' ' '; echo
exit $rc

Recorded output:

base my-commit dirty

Standard error from the same run:

fatal: ambiguous argument 'origin/nosuchbranch': 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 is the status recorded in the log for TE-301-D.

TE-301-A: by the setup, the remote branch had one commit that the working copy did not (remote-change), the branch had one commit of its own (my-commit), f.txt held an uncommitted edit and untracked.txt was untracked. After git fetch and git reset --hard origin/main, HEAD and origin/main print the same short hash, the log lists remote-change and base only, f.txt reads base and then remote-change, and untracked.txt still holds scratch.

TE-301-B: after the same reset, git reset --hard ORIG_HEAD brought my-commit back. f.txt then reads base and my-commit, without the dirty line, so the uncommitted edit was not restored. TE-301-C: without a fetch, the reset moved the branch to base, the only commit origin/main knew about. After git fetch, origin/main showed remote-change as well.

TE-301-D: git reset --hard origin/nosuchbranch exited with status 128 and printed fatal: ambiguous argument 'origin/nosuchbranch': unknown revision or path not in the working tree. The cat after it printed base, my-commit and dirty, so the commit and the uncommitted edit were still there.

Limits

Uncommitted edits discarded by --hard are normally unrecoverable through Git; it is not a general untracked-file deletion tool. The remote runs use a remote named origin and a branch named main, with one new commit on each side.

Source

Evidence

Evidence table

TestWhat it checksExitVerdictLog sha256 (first 12)
TE-05-Agit reset --mixed restores the index to the target commit and keeps working-tree changes0PASS0c537cd1cd69
TE-05-Bgit reset --hard resets tracked files in the working tree and discards uncommitted edits0PASS00099b3ae2cc
TE-05-Cgit reset --soft moves the branch pointer and leaves index and working tree alone0PASSf60ec3a500d5
TE-05-Dthe reset target need not be an earlier commit: reset can move the branch forward0PASS8865a3f1a0c2
TE-05-Eexpected non-zero exit: git reset --hard with a name that is not a commit stops with a fatal error and changes nothing128PASSa317cb5829cf
TE-301-Agit fetch then git reset --hard origin/main moves the branch to the remote commit and leaves an untracked file alone0PASS45e5f7caafb6
TE-301-Bgit reset --hard ORIG_HEAD brings back the commit that the reset removed, but not the uncommitted edit0PASS4b432347b745
TE-301-Cwithout git fetch first, git reset --hard origin/main uses the old origin/main0PASSfb30f4c37b94
TE-301-Dexpected non-zero exit: git reset --hard to a remote branch that does not exist stops with status 128 and keeps the commit and the edit128PASS5264f33a4837

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.

Questions

revert vs reset vs restore: which one?

reset moves the branch pointer: soft keeps the index and the working tree, mixed keeps the working tree, hard discards both, and git reset --hard origin/main makes the branch match the remote exactly. revert records a new commit that undoes the target change, covered in git revert: undo a commit by adding a new one. restore unstages a file or discards edits that are not staged, covered in Git Restore: unstage or discard a file.

All full guides: All guides