Sub topic 12
Undoing a bad restack with the reflog
This chapter assumes you can run git rebase --update-refs on a stack, as covered in the earlier chapters. It also assumes you know that a branch is a pointer to a commit. Here you learn how to get a stack back to its pre-rebase state when a restack goes wrong.
Why a restack can be undone
A rebase never edits commits. It builds new commits with new IDs, then moves branch pointers to them. The old commits stay in the repository's object database until Git's garbage collection deletes them. Git also keeps a log of where each pointer used to be, so you can find those old commits and move the pointers back.
That log is the reflog. It is a local, per-reference history of the values a reference has held. Each entry has a position such as feature-2@{1}, the commit ID, and a message saying what caused the change.
Two reflogs matter for a restack:
- The
HEADreflog records every commit you checked out or created, including each step of a rebase. - Each branch has its own reflog, which records only the moves of that branch.
The reflog lives in your clone. It is never pushed, and a fresh clone has none of your history.
What a bad restack looks like
Suppose the stack is main, feature-1, feature-2, and feature-3. You ran this:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainYou resolved a conflict in the middle of the stack, finished the rebase, and then the tests failed. The mistake could be a wrongly resolved conflict, or you might have rebased onto the wrong base. In either case you want all three branches back where they were.
Step 1: Try to abort first
If the rebase is still running, you do not need the reflog. Run this:
git rebase --abortGit moves the refs for the stack branches at the end of the rebase, not while it replays commits. An abort therefore leaves feature-1 and feature-2 exactly where they were, and returns feature-3 to its original commit.
Use the reflog only after the rebase has finished.
Step 2: Read the branch reflog
Look at the history of the branch that looks wrong:
git reflog show feature-2The output looks like this:
c3d4e5f feature-2@{0}: rebase (finish): refs/heads/feature-2 onto 7a8b9c0
9a8b7c6 feature-2@{1}: commit: Wire parser into CLI
b2c3d4e feature-2@{2}: commit: Add parser testsThe top entry is the move made by the rebase. The entry below it, feature-2@{1}, is the commit the branch pointed to before the rebase. The exact message text can vary between Git versions, so read the entries and do not match them by their wording.
To see what a candidate entry contains, inspect it like any other commit:
git log --oneline --graph main..feature-2@{1}
git diff feature-2@{1} feature-2The second command shows what the restack changed in the final tree. After a clean restack onto an unchanged base, the difference is only what the new base added. After a bad conflict resolution, it also shows the lines you got wrong.
Step 3: Find the old tip of every branch
Each branch in the stack has its own reflog, so check all of them:
for b in feature-1 feature-2 feature-3; do
git log -1 --format="$b: %h %s" "$b@{1}"
doneThis prints the commit each branch pointed to one move ago. It is only correct if each branch moved exactly once since the good state. If you committed on a branch after the rebase, or ran the rebase twice, @{1} is not the pre-rebase commit. Read the full reflog and pick the entry by hand in that case.
You can also select an entry by time:
git rev-parse 'feature-2@{2.hours.ago}'This returns the commit the branch pointed to two hours ago. It works when you know roughly when the bad rebase happened.
Step 4: Restore the branches
Reflog positions shift every time a reference moves. After you reset feature-3, feature-3@{1} points at a different entry than before. Capture all the old commit IDs first, then move the branches:
old1=$(git rev-parse 'feature-1@{1}')
old2=$(git rev-parse 'feature-2@{1}')
old3=$(git rev-parse 'feature-3@{1}')
git branch -f feature-1 "$old1"
git branch -f feature-2 "$old2"
git switch feature-3
git reset --hard "$old3"Check the result before you do anything else:
git log --oneline --decorate --graph main..feature-3You should see the original linear stack with all three branch names on their commits, based on the old main.
The two commands differ in what they can do:
| Command | Use it for | Limit |
|---|---|---|
| `git branch -f NAME COMMIT` | Moving a branch that is not checked out | Refuses if the branch is checked out in any worktree |
| `git reset --hard COMMIT` | Moving the branch you are on | Discards uncommitted changes in your working tree |
Stash or commit your work before running git reset --hard.
The restore is a single scripted sequence, so the order of the steps matters. This diagram shows the recovery path:
The single-branch version
For one branch, the example from the plan is enough:
git reflog show feature-2
git branch -f feature-2 'feature-2@{1}'Use this when only one branch in the stack is wrong and the others are fine. Remember that a restack changes the base of every branch above the one you move. If you move only feature-2 back, feature-3 still sits on the new commits and no longer descends from feature-2. Check the graph, and restack feature-3 with git rebase --onto if needed.
When the HEAD reflog helps
The branch reflog only shows where a branch pointed. The HEAD reflog shows everything you did, in order:
git reflogIt records each commit the rebase replayed, which helps in two cases. The first is when you cannot tell which branch entry is the right one, because the HEAD log shows the sequence of commands in order. The second is when you resolved a conflict, continued, and want the commit as it was in the middle of the rebase. Each replayed commit appears as a rebase (pick) entry, and you can create a branch at any of them:
git branch rescue 'HEAD@{5}'Creating a new branch is the safest way to look at an old state, because it does not move any existing branch. Compare rescue with your stack, and delete it with git branch -d rescue when you are done.
Recovery after a force push
If you already ran git push --force-with-lease, the remote branches hold the bad commits. Your local reflog still has the old ones. Restore the local branches as in Step 4, then push again:
git push --force-with-lease origin feature-1 feature-2 feature-3The lease check compares the remote branch with your remote-tracking branch. Your last push updated that tracking branch to the bad commits, so the check passes and the push puts the old commits back. If a teammate pushed to one of the branches in between, the check fails and you must fetch and decide what to keep.
Remote-tracking branches have reflogs too. After a force push, origin/feature-2@{1} is the commit that the remote branch had before your push, as far as your last fetch knew. This is a second place to look if the local branch reflog is missing.
Limits of the reflog
- It is local. A new clone, a different machine, or a deleted repository has no history to recover from.
- Entries expire. By default, reflog entries for commits that are still reachable are kept for 90 days. Entries for commits that are no longer reachable from the branch are kept for 30 days. Configure these with
gc.reflogExpireandgc.reflogExpireUnreachable. After expiry,git gccan delete the old commits permanently. - Deleting a branch deletes its reflog. If you ran
git branch -Don a stack branch, use theHEADreflog to find the commit and recreate the branch. - Uncommitted work is not in the reflog.
git reset --hardremoves changes that were never committed, and no reflog entry brings them back. ORIG_HEADcovers one branch. Git setsORIG_HEADto the tip of the branch you rebased, herefeature-3. It does not record the lower branches, so use the branch reflogs for those.
Recovery checklist
- If the rebase is still running, run
git rebase --abortand stop. - Run
git reflog showfor each branch in the stack and find the entry from before the rebase. - Save the old commit IDs with
git rev-parse, because reflog positions shift as branches move. - Move each branch back with
git branch -f, and usegit reset --hardfor the checked-out branch. - Verify the stack with
git log --oneline --decorate --graph. - If you had pushed the bad stack, push the restored branches with
--force-with-lease.
Exercise
You finished git rebase --update-refs origin/main on feature-3, and feature-1 and feature-2 moved too. The result is wrong. Write the commands that restore all three branches from the reflog, and explain why you capture the commit IDs before you move any branch.
Show answer
Capture the IDs first: old1=$(git rev-parse 'feature-1@{1}'), old2=$(git rev-parse 'feature-2@{1}'), and old3=$(git rev-parse 'feature-3@{1}'). Then run git branch -f feature-1 "$old1", git branch -f feature-2 "$old2", git switch feature-3, and git reset --hard "$old3". Reflog positions shift every time a reference moves, so after the first move @{1} can point at a different entry. Saved commit IDs do not change. Finish with git log --oneline --decorate --graph main..feature-3 to verify the stack.
Exercise
A rebase is stopped halfway with a conflict, and you realize you picked the wrong base. Do you need the reflog? Why or why not?
Show answer
No. Run git rebase --abort. Git moves the stack branches only when the rebase finishes, so an abort leaves feature-1 and feature-2 untouched and returns feature-3 to its original commit.
Exercise
Two weeks ago you restacked and force-pushed. Today you find the restack lost a change. A teammate has also cloned the repository fresh since then. Whose reflog can you use to recover, and what is one reason it might not work?
Show answer
Only your own clone's reflog has the pre-restack positions, because reflogs are local and are not pushed. The teammate's fresh clone has no such history. It might not work if the entries have expired (30 days for unreachable commits by default, 90 for reachable ones) and git gc has pruned the old commits, or if you recloned or deleted the branch and its reflog.