Undoing a git rebase

Undoing a git rebase

The easiest way would be to find the head commit of the branch as it was immediately before the rebase started in the reflog

git reflog

and to reset the current branch to it (with the usual caveats about being absolutely sure before reseting with the --hard option).

Suppose the old commit was [email protected]{2} in the ref log:

git reset --hard [email protected]{2}

In Windows, you may need to quote the reference:

git reset --hard [email protected]{2}

You can check the history of the candidate old head by just doing a git log [email protected]{2} (Windows: git log [email protected]{2}).

If youve not disabled per branch reflogs you should be able to simply do git reflog [email protected]{1} as a rebase detaches the branch head before reattaching to the final head. I would double check this, though as I havent verified this recently.

Per default, all reflogs are activated for non-bare repositories:

[core]
    logAllRefUpdates = true

Actually, rebase saves your starting point to ORIG_HEAD so this is usually as simple as:

git reset --hard ORIG_HEAD

However, the reset, rebase and merge all save your original HEAD pointer into ORIG_HEAD so, if youve done any of those commands since the rebase youre trying to undo then youll have to use the reflog.

Undoing a git rebase

Charless answer works, but you may want to do this:

git rebase --abort

to clean up after the reset.

Otherwise, you may get the message “Interactive rebase already started”.

Leave a Reply

Your email address will not be published.