Rebase branch on other branch in past

153 Views Asked by At

I’ve got the following situation:

      develop
------o
       \
        \            master
---o-----o-----o-----o-----o-----o-----o
    \               /      A           B
     \             /
      o-----o-----o

Here, I accidentally developed feature branches A and B on top of master instead of develop (B branches off from A). In addition, master actually contains changes (the side-branch, and more changes to the left of my little ASCII art) which do not belong in develop, and are correct where they are.

How can I fix this repository so that A and B are rebased onto develop? The result should look like this (dotted branch and dotted arrow denote rebase operation):

      develop
------o-----o-----o-----o  <· · · ·
       \    A           B           ·
        \                             ·
---o-----o-----o-----o - - ◦ - - ◦ - - ◦
    \               /master
     \             /
      o-----o-----o

I thought that this was a simple rebase --onto develop master A but that resulted in the following situation instead:

      develop
------o-----o
       \    A
        \                 (*)
---o-----o-----o-----o-----o-----o
    \               /master      B
     \             /
      o-----o-----o

… where (*) is the old changeset A, and A is now a copy of that changeset.

I have also tried the same without --onto, but this resulted in an unmerged changesets which, upon inspection, turned out to be from the repository’s very-far distant past. Skipping all these changesets then resulted in an additional, completely broken branch.

1

There are 1 best solutions below

0
On BEST ANSWER

This should be solvable by using git cherry-pick. You should be able to cherry pick the commits you want from master into develop:

git checkout develop
git cherry-pick master..B

After this, the commits for A and B are copied to develop. All that remains is to rename the branches.