Sub topic 11
Undoing a rebase with ORIG_HEAD and the reflog
This chapter assumes you can read git log output, know that a branch is a movable pointer to a commit, and have rebased a branch at least once. A rebase creates new commits and moves the branch pointer to them. The old commits stay in the repository for a while, so a bad rebase can almost always be undone.
Why the old commits survive
Rebase does not edit commits. It replays them as new commits with new hashes, then moves the branch name to the last new commit. The original commits lose their branch name, but Git does not delete them right away. Git removes unreachable commits only during garbage collection, and by default that happens weeks after they become unreachable.
Two records help you find them:
ORIG_HEADis a reference that Git sets to the previous position ofHEADbefore risky operations such as rebase, merge, and reset.- The reflog is a local log of every position
HEADand each branch has held.
Undo with ORIG_HEAD
Right after a rebase, ORIG_HEAD points to where your branch was before it started. Reset the branch to it:
git reflog
git reset --hard ORIG_HEADgit reset --hard moves the branch pointer and overwrites your working tree. It discards uncommitted changes, so commit or stash them first.
ORIG_HEAD is overwritten by the next operation that sets it. If you ran another rebase, merge, or reset after the bad one, it no longer points where you need. Use the reflog instead.
Find the right commit in the reflog
git reflog lists recent positions of HEAD, newest first:
c3f9a12 HEAD@{0}: rebase (finish): returning to refs/heads/feature
c3f9a12 HEAD@{1}: rebase (pick): Add session tests
9d41be7 HEAD@{2}: rebase (start): checkout origin/main
5e0c7a8 HEAD@{3}: commit: Add session testsThe entry just before rebase (start) is the state before the rebase. Here that is 5e0c7a8 at HEAD@{3}. Reset to it by hash or by reflog name:
git reset --hard 5e0c7a8
# or
git reset --hard HEAD@{3}If you only want to inspect the old state first, create a branch there. This changes nothing else:
git branch feature-backup 5e0c7a8
git log --oneline feature-backupCommon errors
- Resetting with uncommitted work.
--harddiscards it. Stash or commit first. - Using a stale
ORIG_HEAD. Checkgit log ORIG_HEADbefore resetting. If it does not show your old commits, use the reflog. - Counting
HEAD@{n}after more activity. The numbers shift with every new entry. Use the hash. - Expecting the reflog on another machine. The reflog is local. A fresh clone has none of your history.
- Waiting too long. Reflog entries expire (90 days by default for reachable entries, 30 days for unreachable ones), after which garbage collection may remove the commits.
- Undoing after a push. Resetting locally does not change the remote. If you already force-pushed the rebased branch, you must push the restored branch again with
git push --force-with-lease.
Exercise
You rebased feature, then ran git merge other on the same branch, and now ORIG_HEAD points to the pre-merge state. How do you return to the state before the rebase?
Show answer
Run git reflog, find the entry just before rebase (start), and note its hash. Then run git reset --hard <hash>. ORIG_HEAD was overwritten by the merge, so it cannot be used here.
Exercise
Why does git reset --hard ORIG_HEAD restore the pre-rebase commits even though the rebase "rewrote" them?
Show answer
Rebase creates new commits and moves the branch pointer. It never edits or deletes the original commits. ORIG_HEAD still points to the old tip, so resetting the branch to it makes the old commits the branch history again.