Git: Undo a Commit
Last updated
How you undo a commit depends on which commit it is and whether it's been pushed. An older commit buried in your history - or anything already shared - calls for git revert, which adds a new commit that reverses it without rewriting anything. A run of recent local commits can simply be reset away with git reset. If all you need is to undo the most recent commit, the git undo last commit page covers every variant of that quick fix.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Which command to use
| Situation | Command |
|---|---|
| Undo one older commit, keep everything after it | git revert <hash> |
| Undo a commit you already pushed | git revert <hash> |
| Undo the last 3 local commits, keep the work | git reset --soft HEAD~3 |
| Undo the last 3 local commits, discard the work | git reset --hard HEAD~3 |
| Undo a range with new reverse commits | git revert HEAD~3..HEAD |
reset vs revert
| Behavior | git reset | git revert |
|---|---|---|
| Rewrites history | Yes | No |
| Safe on pushed commits | No | Yes |
| Creates a new commit | No | Yes |
| Works on old commits mid-history | No | Yes |
| Can discard changes | Yes (--hard) | No |
Worked example
Undo a specific older commit - even one that's already pushed - without touching the commits that came after it.
| Step | Command | Result |
|---|---|---|
| 1 | git log --oneline | Find the hash of the bad commit (e.g. a1b2c3d) |
| 2 | git revert a1b2c3d | A new commit reverses it; later commits are untouched |
| 3 | git push | Share the fix - no history was rewritten |
Git undo commit FAQ
How do I undo an older commit without losing the commits after it?
git revert <hash>. Revert creates a new commit that reverses only the one you name - every commit after it stays exactly as it was. Find the hash with git log --oneline first. If the revert conflicts with later changes, Git pauses so you can resolve and git revert --continue.How do I undo multiple commits at once?
git reset --soft HEAD~3 removes the last three while keeping their changes staged (use --hard to discard them). For pushed commits, revert the range instead: git revert --no-commit HEAD~3..HEAD reverses all three, then a single git commit records the undo.How do I undo a commit I already pushed?
git revert <hash> instead of reset. Revert creates a new commit that undoes the changes of the target commit, leaving history intact. This is the safe way to undo shared commits - rewriting pushed history with reset forces everyone else to reconcile their copies.