I pushed directly to main
What just happened
Your commits are now on the shared main branch. Teammates may already have pulled them and CI may already be deploying them.
Potential risks
- Untested code may be deployed to production.
- A force push to remove them would break everyone who already pulled.
Before you run anything
- Tell your team before doing anything. Seriously.
- Prefer git revert: it adds a commit that cancels yours, without rewriting history.
- Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix โ it costs nothing.
Recovery steps
- 1
Keep your work on a branch
Save your commits on a feature branch so you can open a proper PR later.
Safe$ git branch feature/my-work - 2
Identify the commits to cancel
List the latest commits on the remote main.
Safe$ git fetch origin$ git log --oneline -n 10 origin/main - 3
Revert them (recommended)
Creates new commits that undo yours, oldest last. History stays intact and nobody's repo breaks.
Caution$ git switch main$ git revert --no-edit abc1234..def5678$ git push origin main - 4
Last resort: rewrite main
Only if NOBODY pulled yet and branch protection allows it. --force-with-lease refuses to push if someone else pushed in the meantime.
Destructive$ git switch main$ git reset --hard origin/main~1$ git push --force-with-lease origin main
These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer