Sub topic 9
Undoing a rebase with ORIG_HEAD and the reflog
This chapter assumes you can start an interactive rebase, read the todo list, and resolve a conflict. You should also know that rewriting history creates new commits and leaves the old ones in the object database. That last fact is what makes recovery possible.
Why a rebase can be undone
A rebase does not edit commits. It builds new commits with new IDs and then moves the branch name to the new tip. The old commits still exist, but no branch points at them. Git keeps them for a while (by default at least 30 days for unreachable commits that the reflog still references, and 90 days for reflog entries pointing at commits that are still reachable from the branch). Garbage collection removes them only after that.
So undoing a rebase means pointing your branch back at the old tip. Git gives you two ways to find that tip.
Aborting while the rebase is running
If the rebase has stopped and you do not want to continue, use abort. It restores the branch to the state before the rebase began.
git rebase --abortUse this when you are partway through, for example during a conflict you do not want to resolve. It only works while the rebase is still in progress.
Recovering a finished rebase with ORIG_HEAD
When a rebase starts, Git saves the previous tip of your branch in a reference named ORIG_HEAD. After the rebase finishes, you can reset the branch to that value.
git log --oneline -n 3 ORIG_HEAD
git reset --hard ORIG_HEADThe first command lets you check that ORIG_HEAD points where you expect. The second moves the branch and your working tree back to the pre-rebase state.
ORIG_HEAD is overwritten by other commands, including git merge, git pull and another git reset. If you ran any of those after the rebase, do not trust it. Use the reflog instead.
Finding the old tip with the reflog
The reflog is a local log of where HEAD and each branch have pointed. Every checkout, commit, reset and rebase step adds an entry.
git reflogThe output looks like this:
a1b2c3d HEAD@{0}: rebase (finish): returning to refs/heads/my-feature
a1b2c3d HEAD@{1}: rebase (pick): Add login tests
9e8f7a6 HEAD@{2}: rebase (fixup): Add login form
3f2a1b9 HEAD@{3}: rebase (start): checkout origin/main
e4d5c6b HEAD@{4}: commit: Add login testsRead from the top, newest first. The entry just before rebase (start) is the last state before the rebase. Here that is HEAD@{4}, commit e4d5c6b. Reset to it:
git reset --hard HEAD@{4}You can also use the commit ID directly, git reset --hard e4d5c6b. Entry numbers shift every time you run a command that changes HEAD, so check git reflog again before you rely on a number.
Choosing the right tool
| Situation | Command |
|---|---|
| Rebase is stopped, you want out | `git rebase --abort` |
| Rebase finished, nothing else has moved `ORIG_HEAD` | `git reset --hard ORIG_HEAD` |
| Rebase finished, other commands ran since | `git reflog`, then `git reset --hard <entry>` |
| You want to keep the new branch and compare | `git branch backup <entry>` |
Creating a branch at the old tip is the safest option when you are unsure. It does not move anything, and it protects the old commits from garbage collection.
git branch before-rebase ORIG_HEAD
git diff before-rebase my-featureAn empty diff means the rebase changed the history but not the final file contents. That is often what you want from a cleanup.
Limits
- The reflog is per repository and local. A fresh clone has no record of your old commits.
- If you already pushed the rewritten branch, resetting locally does not change the remote. You would need to push again, which brings back the concerns covered in the chapter on publishing rewritten history.
- Uncommitted work lost to a
reset --hardis not in the reflog, because it was never committed.
Exercise
You finish a rebase, then run git pull on another branch. You now want to undo the rebase on your feature branch. Why is git reset --hard ORIG_HEAD unreliable, and what do you do instead?
Show answer
git pull can overwrite ORIG_HEAD, so it may no longer point at the pre-rebase tip. Run git reflog, find the entry just before the rebase (start) line for your feature branch, and run git reset --hard with that entry or its commit ID. Check out the feature branch first so the reset moves the right branch.
Exercise
What does git branch before-rebase ORIG_HEAD do, and why use it before a risky reset?
Show answer
It creates a new branch named before-rebase at the commit ORIG_HEAD points to, without moving your current branch or changing the working tree. This keeps a named reference to the old commits, so you can compare with git diff or return to them even if the reflog later changes.