Menu
Coddy logo textTech

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

SituationCommand
Undo one older commit, keep everything after itgit revert <hash>
Undo a commit you already pushedgit revert <hash>
Undo the last 3 local commits, keep the workgit reset --soft HEAD~3
Undo the last 3 local commits, discard the workgit reset --hard HEAD~3
Undo a range with new reverse commitsgit revert HEAD~3..HEAD

reset vs revert

Behaviorgit resetgit revert
Rewrites historyYesNo
Safe on pushed commitsNoYes
Creates a new commitNoYes
Works on old commits mid-historyNoYes
Can discard changesYes (--hard)No

Worked example

Undo a specific older commit - even one that's already pushed - without touching the commits that came after it.

StepCommandResult
1git log --onelineFind the hash of the bad commit (e.g. a1b2c3d)
2git revert a1b2c3dA new commit reverses it; later commits are untouched
3git pushShare the fix - no history was rewritten

Git undo commit FAQ

How do I undo an older commit without losing the commits after it?
Use 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?
For local-only commits, 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?
Use 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.
When should I use reset and when should I use revert?
Reset when the commits are yours alone and still local: it simply moves the branch back, as if they never happened. Revert when the commit is pushed, shared, or sits in the middle of history: it undoes the changes with a new commit that's safe for everyone to pull.
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