Keep branch structure during git rebase - Stack Overflow

admin2025-04-29  2

Let's say I have the following tree:

A -- B -- C -- D -- E       main
           \
            F -- G -- H     featureA
                  \
                   I        featureB

I'm working off featureA and want to update to latest main. If I git checkout featureA followed by git rebase main, it does not bring featureB branch along with it.

Here is what I want:

A -- B -- C -- D -- E                    main
                     \
                      F' -- G' -- H'     featureA
                             \
                              I'         featureB

Let's say I have the following tree:

A -- B -- C -- D -- E       main
           \
            F -- G -- H     featureA
                  \
                   I        featureB

I'm working off featureA and want to update to latest main. If I git checkout featureA followed by git rebase main, it does not bring featureB branch along with it.

Here is what I want:

A -- B -- C -- D -- E                    main
                     \
                      F' -- G' -- H'     featureA
                             \
                              I'         featureB
Share Improve this question edited Jan 7 at 0:18 Schwern 166k28 gold badges219 silver badges365 bronze badges asked Jan 6 at 23:42 xormapmapxormapmap 493 bronze badges 6
  • 1 I’m not a Git expert by any stretch, but wouldn’t it make more sense to rebase featureA from main, then rebase featureB from featureA? – esqew Commented Jan 6 at 23:44
  • If you take the opportunity to also update featureB, make its parent H', it's simpler. – Schwern Commented Jan 7 at 0:23
  • This question is similar to: How can I rebase multiple branches at once?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – mkrieger1 Commented Jan 7 at 0:47
  • This question is similar to: Git squash commits in middle of branch with child branches after that. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – j6t Commented Jan 7 at 7:39
  • 1 The problem is that you want to update two refs which are unrelated. Check out featureA, make a temporary merge of featureB (just -s ours), then git rebase --update-ref --rebase-merges main, then reset back to delete the temporary merge. This way you get both refs in the same rebase operation. I’m pretty sure that Johannes Sixt (j6t) has an answer like that somewhere but I wasn’t able to find it. – Guildenstern Commented Jan 7 at 17:28
 |  Show 1 more comment

1 Answer 1

Reset to default 1

That has to be done by hand:

git rebase main featureA
git rebase G festureB --onto G'

You could use the old commit ID of featureA (that is, before it was rebased) instead of G in the second command or you could create a temporary branch where featureA is before the rebase so that you can use that branch after featureA is rebased.

转载请注明原文地址:http://anycun.com/QandA/1745939024a91393.html