You pushed a broken commit to shared main — undo it safely without rewriting history
A commit you already git pushed to the shared main branch broke the build, and other engineers have pulled it. Undo its changes without rewriting or removing any published commit, and without a force-push.
Constraints: keep the shared history intact (no reset, no push --force); the fix must be a normal commit teammates receive with a plain git pull; assume the bad commit's hash is a1b2c3d.
# main already contains the pushed bad commit a1b2c3d
git ???
Give the command sequence that undoes it safely.
Add an inverse commit with git revert a1b2c3d, then git push. revert records the reverse diff of the bad commit as a new commit, so the broken change is undone while published commits stay in place. You add history, not rewrite it, so teammates just git pull — no force-push. Use reset or push --force only on local commits.
- ✗Reaching for
reset --hardpluspush --forceon a shared branch - ✗Thinking
revertdeletes the commit instead of adding an inverse one - ✗Using
commit --amendto fix an already-pushed commit
- →How does reverting a merge commit differ, and why does it need
-m? - →When is rewriting history with
resetactually acceptable?
Solution
git revert does not delete the commit; it creates a new commit with the reverse diff, so the bad change is undone while the published history stays intact.
git revert a1b2c3d # creates a new commit that undoes a1b2c3d
git push # a normal push — no force needed
commit; the original commit stays in history.
else's clone — acceptable only for local, unpublished commits.
revertcomputes the reverse diff of commita1b2c3dand records it as a new- Because history is added, not rewritten, teammates only need
git pull. reset --hard+push --forcewould rewrite shared history and break everyone
If a message editor opens, pass --no-edit to accept the default revert message.