Menu
Coddy logo textTech

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.

Undoing the last commit moves HEAD back one step - the mode decides whether the changes stay staged, unstaged, or are discarded.

Pick what to do with the changes

GoalCommand
Undo commit, keep changes stagedgit reset --soft HEAD~1
Undo commit, keep changes unstagedgit reset HEAD~1
Undo commit, discard changesgit reset --hard HEAD~1
Fix the last commit (message or files)git commit --amend
Undo a commit you already pushedgit revert HEAD

Worked example

Undo the last commit and re-commit it with a better message.

StepCommandResult
1git reset --soft HEAD~1Commit removed, changes stay staged
2git 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?
Run 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?
Run 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?
Use 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?
Use 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.
Can I practice this online?
Yes. Open the terminal playground to run these commands in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers undoing changes step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED