The Complete Guide to Undoing Things in Git: amend, reset, revert & reflog

Fixing a typo in a commit message, dropping a commit that should never have existed, discarding local edits, or recovering after an over-aggressive reset — the right command depends on three questions: which layer the change sits in (working tree / index / commit), whether it is pushed, and whether collaborators exist. Every command below was run in a throwaway repository, with output shown inline.

The Scenario Matrix

Scenario Command Risk
Just committed, typo in message git commit --amend Unpushed = zero risk
Undo an unpushed commit, keep changes git reset --soft/--mixed HEAD~1 Zero risk (recoverable via reflog)
Undo an unpushed commit, discard everything git reset --hard HEAD~1 Uncommitted tracked changes are lost too
Undo a pushed commit git revert <hash> Zero risk (adds an inverse commit)
Recover commits after a reset git reflog + git branch rescue <hash> Expires (90 days by default)
Accidentally deleted an untracked new file Git cannot help It never entered the object database

The last row matters most: Git can only recover content that entered the object database. Untracked files (created but never git added) are beyond recovery once deleted.

amend: Fix the Last Commit

git commit --amend folds the current index and a new message into a brand-new commit replacing the last one:

echo v1 > a.txt && git add . && git commit -qm "initail commmit"   # typo
git commit --amend -qm "initial commit"
git log --oneline        # → ad04563 initial commit (hash changed)

The typo is fixed and the hash is completely different — amend means "a new commit replaces the old one", not "edit". It is therefore only for unpushed commits; amending a pushed one is history rewriting.

amend also folds in whatever is currently staged: if the index holds changes you do not want committed, unstage them with git reset first.

reset: Where the Changes Land in Each Mode

git reset moves the branch pointer back to a target commit; the modes differ in where the working-tree and index changes end up. Tested by committing a.txt, then b.txt and c.txt, then walking back from add-c one mode at a time.

Mode HEAD Index Working tree Tested git status
--soft HEAD~1 Back 1 Kept (c.txt staged) Kept A c.txt
--mixed HEAD~1 (default) Back 1 Cleared Kept (unstaged) ?? b.txt ?? c.txt
--hard HEAD~1 Back 1 Cleared Discarded (tracked files) Clean

One-line selection: soft to re-organize commits, mixed to re-edit, hard to truly discard.

An easy edge to miss: reset --hard does not delete untracked files. b.txt became untracked after mixed and survived the hard reset — it is outside both the object database and the index. To clear those too, use git clean -fd (preview with git clean -nd). A hard reset therefore does not guarantee a clean working tree; it only returns tracked files to the target commit.

revert: The Only Safe Undo for Pushed Commits

git revert <hash> moves no pointers — it computes the inverse patch and creates a new commit:

git revert --no-edit HEAD
# → [main 47d7e82] Revert "add-b"
git log --oneline | head -2
#   47d7e82 Revert "add-b"
#   6c3764a add-b

b.txt added by add-b is removed, and both commits remain — nothing was rewritten, and everyone who pulls lands in a consistent state. The cost is an extra "undo record", which is exactly where auditability comes from.

The rule follows: pushed (especially shared) → revert; local only → reset. Resetting a shared branch and force-pushing imposes your history on every collaborator — one of the most common team incidents (see the golden rule in Git Rebase vs Merge).

reflog: Recovering Commits You Thought Were Gone

Commits removed by git reset --hard do not vanish immediately — git reflog records every HEAD movement (kept 90 days by default). The tested recovery flow:

git reset --hard HEAD~1          # oops: dropped add-b
git log --oneline | head -1      # → 6c3764a add-b (current HEAD)
git reflog | head -4
#   6c3764a HEAD@{0}: reset: moving to HEAD~1
#   47d7e82 HEAD@{1}: revert: Revert "add-b"   ← the commit to recover is still here
git branch rescue 47d7e82        # one command to recover
git log rescue --oneline | head -1
# → 47d7e82 Revert "add-b" ✓

Three caveats: the reflog is local and is not transferred by push, so teammates have no record of your movements; expired entries are garbage-collected; and uncommitted changes never appear in it — they never entered the object database, which is the real meaning of "commit first, then experiment".

If you must force-push, use --force-with-lease rather than --force: it refuses to overwrite when the remote moved since your last fetch.

Reference: Git commands and the git-memo tool.

Frequently Asked Questions

Can I recover my code after git reset --hard?

Committed work: yes — git reflog records every HEAD movement; find the pre-reset hash and run git branch rescue <hash> (reflog keeps 90 days by default). Uncommitted work: no — reset --hard wipes tracked working-tree changes with no recovery path (ironically, brand-new untracked files survive, as tested in this article). That is why 「commit first, then experiment」 saves you.

Can I undo commits that are already pushed with reset?

Technically yes, socially no. reset rewrites history and then requires a force push, which scrambles every collaborator's local branch and breaks CI records and issue links. For pushed commits use git revert: it creates an inverse commit, history stays intact, and everyone who pulls lands in a consistent state. One-line rule: reset for private branches, revert for shared ones.

What exactly differs between the three git reset modes?

The difference is which layer the changes land in: --soft moves only HEAD, keeping changes staged (for re-organizing commits); --mixed (default) moves HEAD and unstages, keeping changes in the working tree (for re-editing); --hard moves HEAD and discards all tracked changes (for truly giving up — with care). The tested comparison is in the table in this article.

Does git commit --amend create a new commit?

It creates a brand-new commit replacing the old one — possibly identical content, but a completely different hash (the timestamp changes). For unpushed commits this is a safe edit; for pushed ones, push gets rejected afterwards and needs force-pushing with all the collaborator problems that entails. Also note: amend folds whatever is currently staged into the commit — check the staging area first.

← Back to Blog