Undo rebase - Anjum219/Git-basics GitHub Wiki

In a git branch, we may have some commits rebased on top on another branch. And after some stories, the branch on which we rebased on can get stale. And we want to undo rebase.

Let's get our hands dirty!

git reflog

The ref logs are listed as rows of commit sha, head tips etc.

After you do reflog, copy the HEAD@{x} when HEAD@{x} points to the head of the stale branch on top of which you rebased your current commits.

Now we will reset current head to HEAD@{x}. Before that, if there is any change we want to commit, please commit them, so that we don't lose them after resetting head. After resetting head, we can apply required commits using cherry-pick.

So, let's say we want to reset to HEAD@{21} and want to apply 2 commits with sha 123 and 456, where 456 should be on top of 123-

git reset --hard HEAD@{21}
git cherry-pick 123
git cherry-pick 456

Ignore the cherry-picks if you only need to fix the rebase.