A bad commit is already on shared `main`. Others have pulled it. How do you undo it?
IntermediateScenarioUndoing Mistakes
Answer
Situation read → Rewriting shared history would break everyone's clones.
What I'd do →
git revert <bad-sha>creates a new commit that inverts the change.- Push normally:
git push origin main. - For several commits:
git revert --no-commit A..B && git commit. - Open a ticket to fix the root cause properly, then reintroduce the change.
Why → Revert adds history instead of removing it, so every clone stays compatible.
Interview tip → "Revert shared, reset private" is the one-line rule.
More on Undoing Mistakes
You committed on `main` when you meant to be on a feature branch. Nothing is pushed. Fix it.BeginnerYou made a typo in your last commit message and have not pushed.BeginnerYou ran `git reset --hard` and lost uncommitted-then-committed work. Can you get it back?IntermediateYou need to revert a merge commit on `main`. `git revert` says "commit is a merge but no -m option was given".IntermediateYou deleted a local branch with unmerged work by mistake.BeginnerAfter `git stash pop` you got conflicts. Is your stash lost?Intermediate