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 →
- If still in progress:
git rebase --abort. - If finished:
git reflogand find the entryrebase (start); take the one just before it. git reset --hard HEAD@{N}, or useORIG_HEADstraight 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.
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.BeginnerA bad commit is already on shared `main`. Others have pulled it. How do you undo it?IntermediateYou 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.Beginner