Interview Prep/Git & GitHub/Undoing Mistakes

A teammate force-pushed to `main` and your commits vanished from the remote. How do you recover?

AdvancedScenarioUndoing Mistakes
Answer

Situation read → The remote branch pointer moved; the old commits still exist in clones and, for a while, on the server.

What I'd do →

  1. Check your clone: git reflog show origin/main shows the old remote tip.
  2. Restore the branch pointer: git push origin <old-sha>:refs/heads/main --force-with-lease. Coordinate first.
  3. On GitHub, the push activity or Events API shows the previous SHA: gh api repos/ORG/REPO/events.
  4. Add branch protection to block force pushes to main.

Why → Anyone with an older clone holds the objects, so the data is rarely truly lost.

Interview tip → Use --force-with-lease, never plain --force, and block both on protected branches.

All Git & GitHub questions Roadmap: Git & GitHub

More on Undoing Mistakes