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.
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.
Actually, rebase saves your starting point to
ORIG_HEAD
so this is usually as simple as:However, the
reset
,rebase
andmerge
all save your originalHEAD
pointer intoORIG_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.Charles's answer works, but you may want to do this:
to clean up after the
reset
.Otherwise, you may get the message “
Interactive rebase already started
”.In case you had pushed your branch to remote repository (usually it's origin) and then you've done a succesfull rebase (without merge) (
git rebase --abort
gives "No rebase in progress") you can easily reset branch using command:Example:
Following the solution of @Allan and @Zearin, I wish I could simply do a comment though but I don't enough reputation, so I have used the following command:
Instead of doing
git rebase -i --abort
(note the -i) I had to simply dogit rebase --abort
(without the -i).Using both
-i
and--abort
at the same time causes Git to show me a list of usage/options.So my previous and current branch status with this solution is:
Using
reflog
didn't work for me.What worked for me was similar to as described here. Open the file in .git/logs/refs named after the branch that was rebased and find the line that contains "rebase finsihed", something like:
Checkout the second commit listed on the line.
Once confirmed this contained my lost changes I branched and let out a sigh of relief.