A rebase went wrong? Reset the branch to ORIG_HEAD or to its entry in the reflog to get back exactly where you were before, even after the rebase has finished.
The short answer
#If the rebase is still in progress, run git rebase --abort. If it has already finished, reset the branch to where it was before the rebase. Straight afterwards, ORIG_HEAD points there; at any later time, the branch's reflog does.
_10git rebase --abort # rebase still in progress_10git reset --hard ORIG_HEAD # rebase just finished_10git reset --hard feature@{1} # or: the branch's previous position from its reflog
Why it happens
#A rebase does not edit your existing commits. It creates new copies of them on top of another base and then moves your branch pointer to the last copy. The original commits are still in the repository; nothing points at them any more, so they disappear from git log.
Git keeps two records that let you find them again:
ORIG_HEAD, which rebase, merge and reset set to the commit you were on before they moved your branch.- The reflog, a local log of every position your branches and
HEADhave been at, with entries kept for weeks by default.
Undoing a rebase is just pointing your branch back at the old commit.
How to fix it
#Abort a rebase that is still running
If Git stopped for a conflict and you would rather not continue, abort. This puts the branch and working tree back to how they were before you ran git rebase.
_10git status # says "interactive rebase in progress" or similar_10git rebase --abort
Reset to ORIG_HEAD straight after the rebase
If the rebase finished and the result is wrong, and you have not run another reset, merge or rebase since:
_10git log --oneline -5 ORIG_HEAD # check it shows your old commits_10git reset --hard ORIG_HEAD
Use the branch reflog
The most reliable option, even days later, is the reflog of the branch itself. A completed rebase moves the branch once, so the entry just before it is the pre-rebase state:
_10git reflog show feature_10# 9c1e2f0 feature@{0}: rebase (finish): refs/heads/feature onto 3a7b1d4_10# 51d0a8e feature@{1}: commit: Add validation to sign-up form_10# ..._10_10git reset --hard feature@{1}
If you have done several things since, read further down the list and pick the commit you want by its hash. You can also look at the HEAD reflog with plain git reflog and find the line just before "rebase (start)".
Keep a backup branch next time
Before a risky rebase, a throwaway branch costs nothing and makes undoing trivial:
_10git branch backup/feature-before-rebase_10git rebase main_10# if it goes wrong:_10git reset --hard backup/feature-before-rebase
If you already force-pushed the rebased branch
After resetting locally, push the restored history back up. Use --force-with-lease so you do not overwrite commits someone else pushed in the meantime:
_10git push --force-with-lease
Common pitfalls
#- Relying on
ORIG_HEADafter running other commands. Any later reset, merge or rebase overwrites it. - Forgetting that
git reset --harddiscards uncommitted changes. Rungit statusfirst and stash anything you want to keep. - Using
HEAD@{1}instead of the branch reflog. During a rebaseHEADmoves many times, soHEAD@{1}is usually one of the intermediate steps, not your original branch. - Waiting too long. Unreachable commits are eventually removed by garbage collection; by default reflog entries for them expire after about 30 days.
- Expecting the reflog on another machine or the remote. It is local to your clone.
Related reading
#Was this resource helpful?