Interview Prep/Git & GitHub/Undoing Mistakes

A rebase went badly and your branch is a mess. How do you return to the state before the rebase?

IntermediateScenarioUndoing Mistakes
Answer

Situation read → Rebase rewrites commits, but the originals still exist.

What I'd do →

  1. If still in progress: git rebase --abort.
  2. If finished: git reflog and find the entry rebase (start); take the one just before it.
  3. git reset --hard HEAD@{N}, or use ORIG_HEAD straight after the rebase: git reset --hard ORIG_HEAD.

Why → ORIG_HEAD is set to the previous tip by rebase, merge and reset.

Interview tip → ORIG_HEAD is overwritten by the next such command, so act immediately.

All Git & GitHub questions Roadmap: Git & GitHub

More on Undoing Mistakes