Git: Undo the Last Commit
Last updated
To undo the most recent commit, decide what should happen to its changes. Keep them staged with git reset --soft HEAD~1, keep them unstaged with a plain git reset HEAD~1, or throw them away with git reset --hard HEAD~1. If the commit is only slightly wrong, git commit --amend fixes it in place. If it's already pushed, use git revert instead of rewriting history. To undo an older commit or several at once, see the git undo commit page.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Pick what to do with the changes
| Goal | Command |
|---|---|
| Undo commit, keep changes staged | git reset --soft HEAD~1 |
| Undo commit, keep changes unstaged | git reset HEAD~1 |
| Undo commit, discard changes | git reset --hard HEAD~1 |
| Fix the last commit (message or files) | git commit --amend |
| Undo a commit you already pushed | git revert HEAD |
Worked example
Undo the last commit and re-commit it with a better message.
| Step | Command | Result |
|---|---|---|
| 1 | git reset --soft HEAD~1 | Commit removed, changes stay staged |
| 2 | git commit -m "clearer message" | Re-commit the same changes |
Git undo last commit FAQ
How do I undo the last commit but keep the changes?
git reset --soft HEAD~1. This removes the last commit but leaves its changes staged, so you can re-commit them right away. For the changes to sit unstaged in your working tree instead, use git reset HEAD~1 (the default mode).How do I undo the last commit and delete the changes?
git reset --hard HEAD~1. This removes the commit and discards its changes from your working tree. It's destructive - if you might need the work, use --soft instead, or recover later via git reflog.What if I only need to fix the commit message?
git commit --amend. It reopens the last commit so you can edit its message (and re-stage files if needed) without creating a separate commit. Avoid amending a commit you've already pushed, since it rewrites history.How do I undo the last commit after pushing it?
git revert HEAD. Revert creates a new commit that reverses the last one, leaving history intact - the safe choice for shared branches. Resetting a pushed commit forces everyone else to reconcile a rewritten branch.