Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover from inadvertent skip during rebase

Tags:

git

rebase

I just tried to rebase a very old branch with a minor modification onto my master. There was a problem with merging just one of the three files involved, so I did an unthinking --skip, thinking that it would just skip that file, but as it happened, it seems to have skipped all my changes, and rolled forwards. So now the rebase is finished, and my changes seem to have disappeared.

I've seen the question about undoing rebase, but it's all greek to me, I see the reflog, but I don't know which commit the branch was attached to before the rebase.

In any case, I don't really need to undo the rebase, I just want to be able to recover the changes in the two files. Is there anyway to do this properly (failing this, I'll just have to restore yesterday's backup of my repository and pick the bits out by hand).

like image 537
Benjol Avatar asked Mar 12 '10 09:03

Benjol


People also ask

What happens if you git rebase skip?

If you run rebase --abort at a later conflict during the same rebase, the skipped commit will be reverted too of course. If your change already existed upstream, Git will not be able to apply your commit (but usually should skip it automatically, if the patch is exactly the same).

How do I undo a git squash?

How to Undo a Merge Commit in Git. You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog .


1 Answers

First, make a tarball of your git working folder. This makes it easier to try it several times.

Lets assume the following happened

  • git checkout another-old-branch
  • git rebase master
  • some problems (which you skipped)

at this point you are now still in another-old-branch and your reflog shows you:

6f8348f HEAD@{0}: rebase: <commit message of last commit in another-old-branch> e547ec0 HEAD@{1}: checkout: moving from another-old-branch to e547ec0d2a558d189464fc57192066b34ec5f28f^0 65cedf8 HEAD@{2}: checkout: moving from master to another-old-branch 

Imagine that branchs are like symlinks (or pointers), all we have to do is let the branch 'another-old-branch' point back on the old commit-id. the old commit is is still there, and it wasn't touched by your rebase. kinda: 'hey git, another-old-branch is e547ec0d2, forget everything else that happened'

In our case here that was e547ec0d2a558d189464fc57192066b34ec5f28f, so what we have to do now is

  • git checkout another-old-branch # if you aren't already there
  • git reset --hard e547ec0d2a558d189464fc57192066b34ec5f28f

now your branch is back to normal. And you can retry your rebase.

Please note that your reflog is by now a little bit more complicated than aboves example. but it should stil lbe there somewhere...

good luck!

like image 50
reto Avatar answered Sep 19 '22 13:09

reto