Undo a pushed Git commit safely: revert, don't reset
Short answer: To undo a commit that's already been pushed to a shared branch like main, use git revert <commit>. It creates a new commit that reverses the bad one, so history stays intact and teammates can pull normally. Avoid git reset followed by a force-push on shared branches: it rewrites history that other people have already built on and causes confusing conflicts. Reset is fine for commits only you have. Reverting a merge needs the -m 1 option to say which side to keep.
The situation
Someone pushes a change to the main branch and it breaks something: the contact form stops sending, a price shows wrongly, the build fails. The fastest way back to a working state is to undo that commit. But how you undo it matters once other people, or your deploy system, have already pulled it.
Git offers two very different tools:
git revertadds a new commit that does the opposite of an earlier one.git resetmoves the branch pointer back, as if later commits never happened.
Why revert is the right default for shared branches
Git's documentation describes revert's purpose directly: it's used to record new commits that reverse the effect of earlier commits, often a single faulty one. The history ends up showing both the original change and its reversal, which is exactly what you want on a branch other people use:
- Teammates pull the revert like any other commit. Nothing about their local copies breaks.
- Your deploy pipeline sees a normal new commit and deploys it.
- Anyone reading history later sees what happened and why.
- If the change was actually fine and something else was wrong, you can revert the revert.
Reset, by contrast, rewrites the branch. If you reset main back two commits and force-push, everyone who pulled those commits now has history the server doesn't. Their next pull produces odd merges or brings the "removed" commits back. The Pro Git book's chapter on rewriting history makes the general rule clear: rewrite only work you haven't shared yet.
How to revert a pushed commit
- Make sure your working tree is clean and you're up to date:
git pull. - Find the commit:
git log --oneline. - Revert it:
git revert a1b2c3d. Git opens an editor for the commit message; explain why ("Revert price rounding change: totals wrong on orders over 10 items"). - Push:
git push.
To revert several commits, list them, or revert a range. Reverting from newest to oldest reduces conflicts. Adding --no-commit lets you stage several reversals and commit them together as one.
Reverting a merge commit
If the bad change came in through a merged pull request, you'll be reverting a merge commit. The revert documentation explains the difficulty: Git can't know which side of the merge should be considered the mainline, so you must say. In almost every case, the mainline is parent 1, the branch you merged into:
git revert -m 1 <merge-commit>
This undoes everything the pull request brought in. One catch: if the same branch is fixed and merged again later, Git considers its original commits already merged. You may need to revert the revert first, then merge the fix, so the original work comes back along with the correction.
When reset is fine
Reset is the right tool when the commits haven't left your machine, or only exist on a personal branch nobody else uses:
git reset --soft HEAD~1undoes the last commit but keeps its changes staged, handy for fixing a commit message or splitting work.git reset --hard HEAD~1throws the last commit and its changes away entirely. Use with care.
If you do need to rewrite a pushed personal branch, prefer git push --force-with-lease, which refuses to overwrite the remote if someone else has pushed in the meantime.
When the bad commit contained a secret
Reverting removes a password or API key from the current code, but it stays in history. If a credential was committed, treat it as exposed: rotate it with the provider straight away, then decide separately whether to clean history. The revert is still worth doing, but it isn't the fix.
Telling the team
A revert on a shared branch is a small event worth announcing. Post a short message in your team chat with the commit you reverted, why, and what happens next: who will rework the change and whether anything already deployed needs checking, such as orders placed while the bug was live. That saves colleagues from rediscovering the problem, re-merging the faulty branch by accident, or wondering why their feature disappeared.
Protecting main from the next mistake
- Turn on branch protection so
maincan't be force-pushed or deleted. - Require pull requests and passing checks before merging.
- Keep commits small, so reverting one doesn't undo unrelated work.
- Make sure your deploy process can roll back quickly, so a revert isn't the only safety net.
A worked example
Picture a hypothetical Wellington tour operator whose booking site deployed automatically from main. A Friday commit changed how departure times were displayed and showed some tours an hour early. The developer's first instinct was to reset and force-push, but two colleagues had already pulled and started work. Instead, he ran git revert on the commit, wrote a message explaining the issue, and pushed. The pipeline deployed the fix within minutes, colleagues pulled without trouble, and the time-display change was reworked properly the following week.
FAQ
Does git revert delete the commit?
No. The original commit stays in history. Revert adds a new commit that cancels its changes.
Can I revert a revert?
Yes. Reverting the revert commit reapplies the original change.
What if the revert has conflicts?
Later commits may have touched the same lines. Resolve the conflicts as you would in a merge, then continue with git revert --continue.
Should I use my Git host's revert button?
Many hosts offer one for merged pull requests. It creates a revert in a new pull request, which is a tidy, reviewable way to do the same thing.
If your team needs a safer routine for fixing bad releases, tell us how you deploy and we'll suggest one.