Sub topic 12
Undoing a bad restack with the reflog
This chapter assumes you can run git rebase --update-refs on a stack, resolve conflicts, and read git log. You need Git 2.38 or newer. Here you learn to recover a stack after a restack goes wrong.
Why recovery is possible
A rebase does not edit commits. It creates new commits and moves branch pointers to them. The old commits stay in the repository until garbage collection removes them, which by default happens weeks later for unreachable commits.
The reflog is the record of where each ref pointed over time. Git keeps one reflog per branch, plus one for HEAD. Each entry has a position such as feature-2@{1}, which means "where feature-2 pointed one move ago".
The reflog is local. It is not pushed, and a fresh clone has none of your history.
What a bad restack looks like
Typical failures after git rebase --update-refs origin/main:
- You resolved a conflict wrongly and dropped a change.
- You edited the todo list and deleted a commit by mistake.
- You aborted halfway and are unsure which branches moved.
- One branch moved and another did not.
First, check that no rebase is still running. If git status reports one, run git rebase --abort. That returns every branch to its pre-rebase position, and you may need nothing else.
Find the earlier position
List the history of one branch:
git reflog show feature-2Output looks like this:
9f8e7d6 feature-2@{0}: rebase (finish): refs/heads/feature-2 onto 4c3b2a1
3a4b5c6 feature-2@{1}: commit: Wire parser into CLIEntry @{0} is the current position. Entry @{1} is the commit before the rebase. Before you reset anything, inspect it:
git log --oneline feature-2@{1}
git diff feature-2@{1} feature-2The diff between the old and new tip shows the changes the rebase altered. Some difference is expected, because the new base brings in other work. Look for lines you meant to keep.
Restore one branch
Point the branch back at its earlier commit:
git branch -f feature-2 'feature-2@{1}'Quote the reference, because some shells treat the braces specially. Git refuses git branch -f on the branch you have checked out. For that branch, use git reset --hard 'feature-3@{1}' instead. That command discards uncommitted work, so commit or stash first.
Restore the whole stack
--update-refs moves several branches, so you usually restore all of them. Do this in one pass and check each branch:
git branch -f feature-1 'feature-1@{1}'
git branch -f feature-2 'feature-2@{1}'
git switch feature-3
git reset --hard 'feature-3@{1}'
git log --oneline --decorate --graph main..feature-3The graph should show the original linear stack again. Note that @{1} assumes each branch moved once. If you made other commits afterward, use git reflog show and pick the entry by its message instead of counting.
Using ORIG_HEAD as a shortcut
Right after a rebase, ORIG_HEAD points at the tip the current branch had before it started. For the checked-out branch, this works:
git reset --hard ORIG_HEADORIG_HEAD covers only that one branch, and other Git commands overwrite it. Use the branch reflogs for the lower branches.
Check the remote
If you already ran git push --force-with-lease, the remote has the bad version. Restore locally first, then push again with the same command. The lease protects against overwriting a teammate's work. It does not protect you from your own earlier push.
Exercise
After a rebase with --update-refs, feature-1 is correct but feature-2 lost a commit. Which command puts feature-2 back where it was before the rebase, assuming feature-3 is checked out?
Show answer
Run git reflog show feature-2 to confirm the earlier entry, then git branch -f feature-2 'feature-2@{1}'. This works because feature-2 is not the checked-out branch.
Exercise
Why can't you run git branch -f feature-3 'feature-3@{1}' while feature-3 is checked out, and what do you use instead?
Show answer
Git refuses to force-move the branch that is currently checked out. Use git reset --hard 'feature-3@{1}' after committing or stashing any uncommitted work.