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.
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.
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.txtCommand:
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=scratchExit 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.txtCommand:
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.txtRecorded output:
after_reset=remote-change base
after_undo=my-commit base
base
my-commitExit 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.txtCommand:
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 baseExit 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.txtCommand:
cd work
git fetch -q origin
git reset --hard origin/nosuchbranch; rc=$?
cat f.txt | tr '\n' ' '; echo
exit $rcRecorded output:
base my-commit dirtyStandard 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
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 forwardTE-301-A: git fetch then git reset --hard origin/main moves the branch to the remote commit and leaves an untracked file aloneTE-301-B: git reset --hard ORIG_HEAD brings back the commit that the reset removed, but not the uncommitted editTE-301-C: without git fetch first, git reset --hard origin/main uses the old origin/main
- logs: one recorded log for each row above
- Recorded on: Linux x86_64; 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 |
TE-301-A | git fetch then git reset --hard origin/main moves the branch to the remote commit and leaves an untracked file alone | 0 | PASS | 45e5f7caafb6 |
TE-301-B | git reset --hard ORIG_HEAD brings back the commit that the reset removed, but not the uncommitted edit | 0 | PASS | 4b432347b745 |
TE-301-C | without git fetch first, git reset --hard origin/main uses the old origin/main | 0 | PASS | fb30f4c37b94 |
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 | 128 | PASS | 5264f33a4837 |
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