Suppose that you had started an interactive rebase:
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:
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:
$ 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.