Sub topic 10
Undoing a Rebase with ORIG_HEAD and the Reflog
This chapter assumes you can commit, branch, and switch in Git, and that you have run git rebase --onto <newbase> <upstream> <branch> from the earlier chapters. A rebase rewrites history, so a wrong upstream or a bad conflict resolution is easy to make. Git keeps enough information to put the branch back as it was. This chapter shows where that information lives and how to use it.
Why a rebase can be undone
A rebase does not edit commits. It copies the commits in upstream..branch as new commits on the new base, then moves the branch pointer to the last copy. The original commits stay in Git's object database. Nothing points at them anymore, but they still exist.
Undoing a rebase therefore means moving the branch pointer back to the old tip. The only problem is finding the hash of that tip. Git gives you two ways to find it:
ORIG_HEAD, a special reference that rebase sets to the branch tip from before the rebase.- The reflog, a local log of every value that
HEADand each branch have had.
Case 1: the rebase is still running
If the rebase stopped on a conflict and you want to give up, use --abort. It restores the branch, the index, and the working tree to the state before you ran git rebase.
git rebase --abortYou do not need ORIG_HEAD or the reflog here. After an abort, git status reports a clean branch at the old tip. Abort is only available while the rebase is in progress. Once git rebase --continue has applied the last commit, the rebase is finished and you need one of the next two methods.
Case 2: the rebase finished and ORIG_HEAD is intact
When a rebase starts, Git writes the current tip of the branch into ORIG_HEAD. You can read it like any other reference:
git rev-parse --short ORIG_HEAD
git log --oneline ORIG_HEAD -3To undo the rebase, reset the branch to that commit:
git reset --hard ORIG_HEADA complete example you can run
This script builds a small repository, rebases topic from next onto main, and then undoes the rebase. It needs Git 2.28 or later for git init -b.
set -e
git init -q -b main demo
cd demo
git config user.name Demo
git config user.email [email protected]
echo base > a.txt
git add a.txt
git commit -qm "base"
git switch -qc next
echo n1 > n1.txt
git add n1.txt
git commit -qm "next 1"
git switch -qc topic
echo t1 > t1.txt
git add t1.txt
git commit -qm "topic 1"
echo t2 > t2.txt
git add t2.txt
git commit -qm "topic 2"
git switch -q main
echo m1 > m1.txt
git add m1.txt
git commit -qm "main 1"
git switch -q topic
echo "tip before rebase: $(git rev-parse --short HEAD)"
git rebase --onto main next topic
echo "tip after rebase: $(git rev-parse --short HEAD)"
echo "ORIG_HEAD: $(git rev-parse --short ORIG_HEAD)"
git reset --hard ORIG_HEAD
echo "tip after undo: $(git rev-parse --short HEAD)"
git log --oneline --graph --allThe "tip before rebase" and "tip after undo" lines print the same hash. The "tip after rebase" line prints a different hash, because the copied commits have new IDs. The final graph shows topic back on top of next, with main carrying its own extra commit.
What can overwrite ORIG_HEAD
ORIG_HEAD is a single slot. Several commands write to it, so it is only reliable right after the rebase:
git mergeandgit pullgit reset, including thegit reset --hard ORIG_HEADthat undoes the rebase- another
git rebase
Two consequences follow.
- If you run
git pullor a second rebase between the first rebase and the undo,ORIG_HEADno longer points where you expect. Check it withgit log --oneline -3 ORIG_HEADbefore you reset. - Resetting to
ORIG_HEADsetsORIG_HEADto the commit you just left. Running the same reset again therefore flips the branch back to the rebased version. This works as a quick "redo" if you undo too early.
Case 3: the reflog
The reflog is a local record of where references pointed over time. Git appends an entry each time HEAD or a branch moves because of a commit, checkout, reset, merge, or rebase. Use it when ORIG_HEAD has been overwritten, or when you found the mistake days later.
git reflogA reflog after a finished rebase looks like this (hashes shortened, entries abbreviated):
3c9d1e2 HEAD@{0}: rebase (finish): returning to refs/heads/topic
3c9d1e2 HEAD@{1}: rebase (pick): topic 2
8a47b5f HEAD@{2}: rebase (pick): topic 1
d0e6f13 HEAD@{3}: rebase (start): checkout main
5be21c0 HEAD@{4}: commit: topic 2Read it from the bottom up. The entry 5be21c0 ... commit: topic 2 is the last thing you did before the rebase, so 5be21c0 was the old tip. The rebase (start) line marks the beginning of the rebase. The pick lines are the commits being copied, and finish is where the branch pointer moved.
To undo the rebase, reset to the hash from before rebase (start):
git reset --hard 5be21c0HEAD@{n} versus branch@{n}
git reflog with no argument shows the reflog of HEAD. A rebase of three commits adds a start line, one pick line per commit, and a finish line to it, so HEAD@{1} is a pick, not the old tip. Do not guess a number for HEAD. Read the log.
Each branch has its own reflog. A rebase moves the branch only once, at the end, so the branch reflog has one new entry for the whole rebase:
git reflog show topic3c9d1e2 topic@{0}: rebase (finish): refs/heads/topic onto d0e6f13
5be21c0 topic@{1}: commit: topic 2Here topic@{1} is the old tip. This is the shortcut used in the earlier chapters:
git reset --hard topic@{1}It is correct only if you have not moved topic since the rebase. If you added a commit or rebased twice, topic@{1} points somewhere else. Run git reflog show topic and check the message on the entry before you reset.
Time-based references
Reflog entries can also be addressed by time:
git log --oneline topic@{yesterday} -3
git rev-parse --short "topic@{2.hours.ago}"This is useful when you know roughly when the branch was good but not how many moves have happened since.
Look before you reset
Resetting is a destructive move on the branch pointer, so confirm that you have the right commit first. Three checks cover most mistakes:
# Show the candidate commit and a few ancestors.
git log --oneline -3 ORIG_HEAD
# Compare the candidate with the current branch tip.
git diff --stat ORIG_HEAD topic
# Compare old and new commits one to one.
git range-diff ORIG_HEAD...topicAn empty git diff means the rebase did not change the final tree, only the history. A non-empty diff after a plain rebase usually reveals a conflict resolution, or, with --onto, the difference between the old base and the new one. git range-diff pairs each old commit with its rewritten version and shows what changed in each.
To keep the old and new versions side by side, create a branch at the old tip rather than resetting:
git branch topic-before-rebase ORIG_HEADThis does not touch topic and works even when you are unsure which version you want. Deleting the extra branch later is a single command.
Habit: mark the tip before a risky rebase
Reading the reflog is a recovery tool. A cheaper habit is to name the old tip before you start:
git branch backup/topic
git rebase --onto main next topicIf the result is wrong, git reset --hard backup/topic returns you to it. A branch is also safe from reflog expiry, which matters for the next section.
When recovery stops working
The reflog is not permanent, and it has some limits you should know about.
- Expiry. By default, Git expires reflog entries after 90 days if the commit is still reachable from a branch, and after 30 days if it is not. A commit that became unreachable after a rebase falls under the 30-day rule. After expiry,
git gccan delete it. The settings aregc.reflogExpireandgc.reflogExpireUnreachable. - Local only. Reflogs are not pushed or fetched. A fresh clone has no history of your earlier rebases, and a teammate cannot use your reflog to recover your branch.
- A new clone or deleted repository. If you removed the directory, the reflog went with it.
- Branch deleted. Deleting a branch removes its own reflog. The
HEADreflog still lists the old commits until they expire, so search it withgit reflog. - Uncommitted work. Changes that were never committed or staged are not in the reflog. A hard reset can lose them for good.
After undoing a rebase you already pushed
If you force-pushed the rebased branch and then reset your local copy, the remote still has the rebased history. Your local branch and the remote now differ in the other direction. To restore the remote, push the old history back:
git push --force-with-lease origin topicTeammates who fetched the rebased branch in between will have to repair their copies, so tell them. The remote-tracking reference origin/topic also has a reflog on your machine. It can help you find the previous remote tip if you lost your local copy:
git reflog show origin/topicCommon mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Using `HEAD@{1}` after a rebase | It points at the last copied commit, not the old tip | Use `git reflog show topic` and read the entry |
| Running `git pull` before undoing | `ORIG_HEAD` now refers to the pull | Find the old tip in the reflog |
| Resetting with uncommitted changes | Tracked changes are discarded | Commit or stash first, or use `git reset --keep` |
| Resetting on the wrong branch | Another branch moves back in time | Run `git branch --show-current` first |
| Waiting weeks to notice | Reflog entries expire | Keep a backup branch for long-lived work |
Exercise
You ran git rebase --onto main next topic, it finished cleanly, and then you ran git pull on another branch. Now git reset --hard ORIG_HEAD would move you somewhere unexpected. How do you find the correct pre-rebase tip of topic and restore it without losing uncommitted work?
Show answer
The pull overwrote ORIG_HEAD, so use the reflog instead. Run git status and commit or stash any local changes, or plan to use --keep. Switch to topic and run git reflog show topic. The newest entry is rebase (finish), and the entry just below it, normally topic@{1}, is the old tip. Check it with git log --oneline -3 topic@{1}, then run git reset --keep topic@{1} (or --hard if the tree is clean). If you are unsure, first create a branch with git branch topic-before-rebase topic@{1} to keep the old history without moving topic.
Exercise
A rebase of a three-commit branch finished. Why is git reset --hard HEAD@{1} unlikely to undo it, and what should you use instead?
Show answer
HEAD@{1} is the previous entry in the HEAD reflog, which after a rebase is the last rebase (pick) step, so it is a copied commit on the new base, not the old tip. Use ORIG_HEAD if nothing has overwritten it, or topic@{1} from the branch reflog, which records one move for the whole rebase. Read the entries with git reflog show topic before resetting.