How do I undo a bad rebase in Git?

February 16th, 2013

If you use git and leverage the rebase command, you’ve probably run across a merge issue during the rebase and if you’ve ever felt like, “man, I wish I had a ‘do-over’.

If you’re still in the middle of a rebase it’s easy to start over:

git rebase –abort

But let’s say you started with this:

image

Did a git rebase and are now looking at:

image

Except you screwed up during a merge conflict and now un-sure how you can get your ‘do-over’.

 

I was pleasantly surprised at how easy it was. (If you know key)

Leveraging the git reflog, you can go back in time and check out your branch as though the rebase never happened.

In the following link, I put together a set of steps to create a git repo that puts you into this position (of a bad rebase) and then describes how to get out of it.

https://github.com/staxmanade/GitRebaseReflogFixSample

I’d love to hear any feedback on this repo. Or try submitting a pull request or post a GitHub issue.

Happy Git’ing!

  • bpw1621

    Great post and excellent idea to setup a github playground to follow along with. Quick correction: the last command should be a reset and not a rebase.

    • staxmanade

      Yup! Thanks for the comment. Fixed.

  • http://andypalmer.com Andy Palmer

    This is one of the reasons why it’s better to merge a branch than to rebase. If a merge screws up, you can do a git reset –hard to the previous commit on master and start again, without digging through the reflog.

    Of course, if you’re using git as a local copy of a subversion repository, then rebase is a necessary evil