commit disappeared when using git rebase

Viewed 41

First step

echo "1111111111" >> test.txt && git commit -m "1"
echo "2222222222" >> test.txt && git commit -m "2"
git rebase -i HEAD~2

Next change the configuration similar to the following

s hash 1
p hash 2

Then the git will tell you that you made a mistake

git rebase --edit-todo
//set the configuration similar to the following
//it's worth noting that 'p hash 2' doesn't show up
p hash 1
git rebase --continue

Finally, the second commit disappeared!

Is that normal? Is there any way to get the Git repository back to where it was before git rebase -i HEAD~2 was executed?

1 Answers

It looks like a bug to me. When you say git rebase --edit-todo, you lose 2 because it doesn't show up in the second (edited) todo list, which is the same as asking to drop it — and so when you do the edit and continue the rebase from there, Git does drop it.

However, there is an easy solution: don't say git rebase --edit-todo. When Git stops you and offers to let you edit the todo list, just say git rebase --abort instead and start over, and this time, don't ask to squash the first item in the todo list, which (as you now know) is illegal.

If you already did the edit of the todo list and said git rebase --continue, so that the rebase is now over, you can easily undo everything by looking in the reflog (git reflog). Look for the first commit marked rebase (start) and reset --hard to the commit before that (that is, after it in the list). The one you want to reset to will probably say commit: 2 (or whatever your real last commit message was) so you'll know you're doing the right thing.

Related