How to Undo a Git Commit (The Right Way)
Searching "how to undo a git commit" returns 10 Stack Overflow answers with different commands and no explanation of which to use when. Here is the decision tree.
Step 1: Did you push the commit?
No, it is only local. You have the most options. Use git reset:
# Keep your changes staged (safest — nothing is lost)
git reset HEAD~1 --soft
# Keep your changes unstaged but on disk
git reset HEAD~1 --mixed
# Discard your changes completely (no recovery without reflog)
git reset HEAD~1 --hardYes, it is already pushed. Do not rewrite history on a shared branch. Use git revert instead:
git revert HEAD # undo the most recent commit
git push # push the undo commitRevert creates a new commit that inverts the changes. Your teammates see the history intact — they just also see the fix.
Step 2: Was it multiple commits ago?
If the commit you want to undo is not the latest, you need its hash:
git log --oneline # find the hash
git revert abc1234 # revert that specific commitIf you reverted the wrong commit, just revert the revert: git revert HEAD again will undo the undo.
Step 3: What if you used --hard and lost work?
The reflog still has it. Git keeps a log of every HEAD position change, even across hard resets. Run:
git reflog # look for your commit in the list
git checkout -b recovered abc1234 # put it on a new branchThe reflog retains entries for 30–90 days (configurable). As long as garbage collection has not run, the data is there.
Git Unfucked is a single reference PDF with every recovery scenario mapped out. The right command, the right flags, the edge cases explained — before you need to look them up under pressure.
Get Git Unfucked →