Cheat Sheets · Git Cheat Sheet · Git Undo Last Commit — 3 Safe Ways
Git Undo Last Commit — 3 Safe Ways
git reset --soft HEAD~1Kept, still staged
git reset HEAD~1Kept, unstaged
git reset --hard HEAD~1Deleted
The three resets
| Command | Changes in the commit | Use when |
|---|---|---|
git reset --soft HEAD~1 | Kept, still staged | You want to re-commit with a different message or grouping |
git reset HEAD~1 | Kept, unstaged | You want to re-stage selectively |
git reset --hard HEAD~1 | Deleted | The commit was garbage and so is its content |
Related moves
| Command | Does |
|---|---|
git commit --amend -m "better message" | Rewrite the last commit instead of undoing it |
git reflog | Time machine — find any commit HEAD has ever pointed to |
git revert <sha> | Undo a commit that was already pushed (safe on shared branches) |
Undoing the last commit has three flavors — the difference is what happens to your changes. Pick by how much you want to keep.
Related Git recipes
FAQ
I ran --hard by mistake — is my work gone?
Probably not. git reflog lists every position HEAD has held; find the commit before the reset and git reset --hard HEAD@{n} back to it. Reflog entries live for ~90 days.
Can I undo a pushed commit with reset?
Do not. Rewriting shared history forces everyone to fix their clones. Use git revert — it creates a new commit that undoes the old one cleanly.
reset vs revert in one line?
reset rewrites history (local work only); revert adds new history (safe to share).
Stop memorizing — build it interactively
Generate the exact command in the Git BuilderFull reference: Git Cheat Sheet