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.