Skip to main content
Chapter 51 of 61 · 1 sections · Page 169

Git: Reflog & Lost Commits

Recover lost commits after erroneous rebases, accidental resets, or bad squashes using git reflog.

Original chapter reference: Chapter 51: Reflog - Restoring commits not shown in git log. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 51.1: Recovering from a bad rebase

Suppose that you had started an interactive rebase:

Source example · PDF page 169
git rebase --interactive HEAD~20

and by mistake, you squashed or dropped some commits that you didn't want to lose, but then completed the rebase. To recover, do git reflog, and you might see some output like this:

Source example · PDF page 169
aaaaaaa HEAD@{0} rebase -i (finish): returning to refs/head/master
bbbbbbb HEAD@{1} rebase -i (squash): Fix parse error
...
ccccccc HEAD@{n} rebase -i (start): checkout HEAD~20
ddddddd HEAD@{n+1} ...
...

In this case, the last commit, ddddddd (or HEAD@{n+1}) is the tip of your pre-rebase branch. Thus, to recover that commit (and all parent commits, including those accidentally squashed or dropped), do:

Source example · PDF page 169
$ git checkout HEAD@{n+1}

You can then create a new branch at that commit with git checkout -b [branch]. See Branching for more information.

Buy me a coffee
Daily Practice
0/0