tags:

views:

7521

answers:

6

Does anybody know how to easily undo a git rebase?

The only way that comes to mind is to go at it manually:

  • git checkout the commit parent to both of the branches
  • then create a temp branch from there
  • cherry-pick all commits by hand
  • replace the branch in which I rebased by the manually-created branch

In my current situation this is gonna work because I can easily spot commits from both branches (one was my stuff, the other was my colleague's stuff).

However my approach strikes me as suboptimal and error-prone (let's say I had just rebased with 2 of my own branches).

Any ideas?

Clarification: I'm talking about a rebase during which a bunch of commits were replayed. Not only one.

+61  A: 

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 HEAD@{5} in the ref log
git reset --hard HEAD@{5}

You can check the history of the candidate old head by just doing a git log HEAD@{5} .

If you've enabled per branch reflogs you should be able to simply do git reflog branchname@{1} as a rebase detaches the branch head before reattaching to the final head. I would double check this, though as I haven't verified this recently.

Charles Bailey
For some reason I'd never tried reflog. That thing's awesome! Thanks for the answer :-)
webmat
Whew! Thanks for SO! This also works if you do a merge you didn't mean to.
Daniel Huckstep
Git reflog is awesome, just remember you can get better formatted output with `git log -g` (tip from Scott Chacon's http://progit.org/book).
karmi
+4  A: 

For multiple commits, remember that any commit references all the history leading up to that commit. So in Charles' answer, read "the old commit" as "the newest of the old commits". If you reset to that commit, then all the history leading up to that commit will reappear. This should do what you want.

Greg Hewgill
True, thanks for the reminder ;-)
webmat
+17  A: 

Resetting the branch to the dangling commit object of its old tip is of course the best solution, because it restores the previous state without expending any effort. But if you happen to have lost those commits (f.ex. because you garbage-collected your repository in the meantime, or this is a fresh clone), you can always rebase the branch again. The key to this is the --onto switch.

Let’s say you had a topic branch imaginatively called topic, that you branched off master when the tip of master was the 0deadbeef commit. At some point while on the topic branch, you did git rebase master. Now you want to undo this. Here’s how:

git rebase --onto 0deadbeef master topic

This will take all commits on topic that aren’t on master and replay them on top of 0deadbeef.

With --onto, you can rearrange your history into pretty much any shape whatsoever.

Have fun. :-)

Aristotle Pagaltzis
+24  A: 

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 you've done any of those commands since the rebase you're trying to undo then you'll have to use the reflog.

Pat Notz
Great, thanks! You just saved me a lot of time hunting for the quickest way to fix a mistaken rebase.
bcat
+6  A: 

I actually put a backup tag on the branch before I do any nontrivial operation (most rebases are trivial, but I'd do that if it looks anywhere complex).

Then, restoring is as easy as "git reset --hard BACKUP".

Alex Gontmakher
+4  A: 

Charles's answer works, but you may want to do this:

git rebase -i --abort

to clean up after the reset. Otherwise, you may get the message: "Interactive rebase already started".

Allan