Sub topic 9
Undoing a rebase or an amend
This chapter assumes you can commit, branch and rebase, and that you can read git reflog output and use selectors such as HEAD@{1}. Rebase and amend both rewrite history. They create new commits and move a branch to them. The old commits are not deleted right away, so the reflog can bring them back.
Why rewriting is recoverable
A rebase replays your commits on a new base. Each replayed commit is a new object with a new hash. The branch then points at the new commits. The original commits still exist, but nothing points at them except reflog entries.
An amend works the same way. git commit --amend builds a replacement commit and moves the branch tip to it. The commit you amended is left behind.
Undoing a rebase with ORIG_HEAD
Git sets ORIG_HEAD to the previous tip before risky operations such as rebase and reset. If the rebase result is wrong, you can return to the old state.
git rebase main
# The result is wrong.
git reset --hard ORIG_HEADThis works only if no other operation has overwritten ORIG_HEAD since the rebase. A second rebase or a reset will replace it.
Undoing a rebase with the reflog
The reflog is the reliable route. A rebase writes several entries, one for each step, so the entry you want is the one just before the rebase started.
git reflog
# a1b2c3d HEAD@{0}: rebase (finish): returning to refs/heads/feature
# a1b2c3d HEAD@{1}: rebase (pick): Add export feature
# 7e8f9a0 HEAD@{2}: rebase (start): checkout main
# 4d5e6f7 HEAD@{3}: commit: Add export featureThe entry HEAD@{3} is the last commit before rebase (start). Check it, then move the branch there.
git show --stat HEAD@{3}
git reset --hard HEAD@{3}Because the HEAD reflog also records the rebase steps, counting entries is error-prone. Read the messages, and do not rely on a fixed number.
You can also use the branch's own reflog. It records only the branch tip, so it has fewer entries.
git reflog show feature
git reset --hard feature@{1}Undoing an amend
After an amend, the pre-amend commit is one entry back. The reflog line reads commit (amend).
git commit --amend -m "Better message"
git reflog -3
# 8c9d0e1 HEAD@{0}: commit (amend): Better message
# 2a3b4c5 HEAD@{1}: commit: Add export feature
git reset --soft HEAD@{1}Here --soft moves the branch back but keeps the index and working tree as they are. Your files stay as they were, and only the branch tip moves. Use --hard only if you want the files to match the old commit too.
Common errors
- Using
--hardwith uncommitted work.git reset --harddiscards uncommitted changes, and the reflog cannot restore them. Commit or stash first. - Trusting
ORIG_HEADtoo long. It is a single slot. Confirm it withgit show ORIG_HEADbefore you reset. - Resetting in the middle of a rebase. If the rebase stopped on a conflict, run
git rebase --abortinstead. It restores the original branch. - Counting the wrong entries. Rebase adds many reflog lines. Search for
rebase (start)and pick the entry before it.
Exercise
You amended a commit and now realise the original message was better. The original commit is at HEAD@{1}. Which command moves the branch back to it and keeps your working files unchanged, and why is that safer than --hard here?
Show answer
git reset --soft HEAD@{1}. It moves only the branch tip and leaves the index and working tree alone, so no uncommitted edits are lost. --hard would also overwrite the working files with the old commit's contents.
Exercise
You ran git rebase main, then git rebase -i HEAD~3 on the same branch. Why might git reset --hard ORIG_HEAD not restore the state before the first rebase, and what should you use instead?
Show answer
The second rebase overwrote ORIG_HEAD with the tip from before it. Use the reflog: find the rebase (start) entry for the first rebase and reset to the entry just before it.