Sub topic 10
Undoing a Rebase with ORIG_HEAD and the Reflog
This chapter assumes you can run git rebase --onto, read git log --oneline --graph, and know that a branch is a pointer to a commit. A rebase feels destructive, but it is almost always reversible. Rebasing creates new commits and moves the branch pointer to them. The old commits stay in the repository until Git's garbage collection removes them, which by default happens weeks later.
Why a rebase can be undone
Suppose topic pointed at commit D before you ran git rebase --onto main next topic. Git copied the commits onto main and then moved topic to the last copy, D'. The original commits are no longer reachable from any branch name, but they still exist. To undo the rebase, you only need to point topic back at D.
Git keeps two records of the old tip:
ORIG_HEADis a special reference. Commands that make large moves, such asrebase,merge, andreset, set it to the previousHEADfirst.- The reflog is a local log of every position a branch or
HEADhas had. Each entry says what changed it.
Undo with ORIG_HEAD
Right after a rebase finishes, ORIG_HEAD is the branch tip from before it started.
# Check the old tip first.
git log --oneline -3 ORIG_HEAD
# Move the current branch back to it.
git reset --hard ORIG_HEADgit reset --hard moves the current branch to the given commit and overwrites your working tree to match. Uncommitted changes are lost, so commit or stash them before you run it.
ORIG_HEAD is overwritten by the next command that sets it. If you ran another rebase, a merge, or a reset after the one you want to undo, it no longer points where you need. Use the reflog in that case.
Undo with the reflog
The reflog numbers its entries from the newest. topic@{0} is where topic is now, and topic@{1} is where it was before its most recent change.
git reflog show topicThe output looks like this:
a1b2c3d topic@{0}: rebase (finish): refs/heads/topic onto 9f8e7d6
5e4d3c2 topic@{1}: commit: Add retry logic
8b7a6f5 topic@{2}: commit: Add config optionA rebase writes several reflog entries for HEAD while it replays commits. For the branch itself, it normally adds one entry when it finishes. So topic@{1} is usually the pre-rebase tip, but check the message before you reset.
# Confirm that this is the commit you expect.
git show --stat topic@{1}
# Restore the branch.
git reset --hard topic@{1}If you did more work after the rebase, count back to the right entry or copy the commit hash from git reflog. You can also keep the current state and create a rescue branch from the old tip. That lets you compare the two before you decide:
git branch topic-before-rebase topic@{1}Common errors
- Resetting the wrong branch.
git reset --hardacts on the branch you have checked out. Rungit statusfirst. - Trusting
ORIG_HEADtoo late. Another rebase, merge, or reset replaces it. Ifgit log ORIG_HEADshows an unexpected commit, switch to the reflog. - Losing uncommitted work. A hard reset discards it. Stash first with
git stash. - Expecting the reflog on another machine. The reflog is local. A fresh clone has none of your old tips.
- Waiting too long. Unreachable commits are pruned after a retention period, 30 days by default for reflog entries that point to commits no longer on any branch. Recover soon.
Check the result
After any undo, confirm the branch is where you intended.
git log --oneline --graph --decorate -10The old commit hashes should be back and the new ' copies should no longer be on the branch. The copies still exist in the reflog until pruning, so you can return to them with the same technique if you change your mind.
Exercise
You ran git rebase --onto main next topic, then immediately realized next was the wrong upstream. Give two ways to restore topic to its pre-rebase state, and say when you must use the second one.
Show answer
Run git reset --hard ORIG_HEAD, or run git reset --hard topic@{1} after checking it with git show. Use the reflog form when another command, such as a second rebase, merge, or reset, has overwritten ORIG_HEAD. Commit or stash uncommitted changes first, because a hard reset discards them.