More Git answers

by Danny
Published 2 min read

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.


_10
git rebase --abort # rebase still in progress
_10
git reset --hard ORIG_HEAD # rebase just finished
_10
git 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 HEAD have 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.


_10
git status # says "interactive rebase in progress" or similar
_10
git 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:


_10
git log --oneline -5 ORIG_HEAD # check it shows your old commits
_10
git 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:


_10
git 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
_10
git 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:


_10
git branch backup/feature-before-rebase
_10
git rebase main
_10
# if it goes wrong:
_10
git 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:


_10
git push --force-with-lease

Common pitfalls

#
  • Relying on ORIG_HEAD after running other commands. Any later reset, merge or rebase overwrites it.
  • Forgetting that git reset --hard discards uncommitted changes. Run git status first and stash anything you want to keep.
  • Using HEAD@{1} instead of the branch reflog. During a rebase HEAD moves many times, so HEAD@{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.
#

Was this resource helpful?