开发者

Recover from inadvertent skip during rebase

开发者 https://www.devze.com 2022-12-22 13:52 出处:网络
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 i

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).


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号