Last Updated: October 12, 2018
·
6.581K
· armiger1

Roll Back an Accidental Git Rebase

Ok, so earlier today I wanted to change something in a previous commit to one of my git repos (none of the changes in this series of commits had been uploaded to a remote repo yet, don't worry!), so I did an interactive rebase. I realized I didn't want to do the change just yet, so I quit my editor without saving, and so thought I had aborted the rebase. Boy, was I wrong. I now had a messed up commit history on my hands and had no idea what to do.

So I headed over to the #git IRC channel over at irc.freenode.net and came back with this little gem:

First off, here's what my original tree looked like before the rebase:

Picture

Also, here's the rebase message that I had:

Picture

I was tired and forgot to delete all the commits that were going to be part of the rebase, so when I hit :q!, I ended up unintentionally rebasing. Here's what that looked like when the rebase was complete:

Picture

Needless to say, I messed up big time. Now what? Well, that's where this tip begins:

My first step was to get a log of all my previous commits.

git reflog

Which gave me the following output:

Picture

From there, I needed to figure out what commit I want to reset my history to. In this case, since my original position in the tree was "cbe2593", I'm going to use HEAD@{10}. You can also use the SHA1 sum of the commit you want to roll back to.

After that, I needed to do a hard reset on my repo, which took me back to the commit I had done just before the rebase.

git reset --hard [commit]

So in this case, I entered:

git reset --hard HEAD@{10}

Volia! Once again I had a clean commit history.

Picture

2 Responses
Add your response

You just saved me wasting a bunch of hours of work :)

over 1 year ago ·

@nuisanceofcats I'm glad this helped you. :)

over 1 year ago ·