← Back to all fixes
Git

Need to undo a commit already pushed to main

The Issue

A bad change was pushed to main. Force-push is not allowed. Need to undo it safely.

Root Cause

The commit introduced a bug or broke production.

How to Fix
  1. Use `git revert` — it creates a new commit that undoes the change, safe for shared branches
  2. `git revert <commit-sha>` — creates the inverse commit
  3. Push normally: `git push origin main`
  4. If the commit is a merge commit: `git revert -m 1 <merge-commit-sha>`
  5. Do NOT use `git reset --hard` on shared branches — it rewrites history and breaks other developers