How to copy commits from one branch to another

后端 未结 2 1772
别跟我提以往
别跟我提以往 2021-02-14 19:36

I have two features I\'m working on

Shared -- Commits -- A -- B -- C Feat1

Shared -- Commits -- D -- E -- F Feat2

The problem is that to test Feat2 I rea

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 20:42

    Merging

    You can merge instead of rebase. Since your combo branch is just for testing, you don't have to worry about the history being a bit messier.

    That is:

    git checkout feat2
    git branch -b combo
    git merge feat1
    

    Later, when you update feat2:

    git checkout combo
    git merge feat2
    

    Whenever you make an update to feat1 or feat2, merge that into combo.

    The disadvantage is that you'll have to merge all commits in both branches. If there are changes you don't want, you'll have to make a separate commit removing those changes in the combo branch.

    Rebasing

    You can rebase the additional commits from feat2 onto combo.

    Suppose you add some commits onto feat2, then to move these commits to combo:

    git rebase --onto combo lastcommittoexcludeonfeat2 feat2
    

    Alternatively, before you make the changes to feat2, add a branch or tag, eg:

    git checkout -b lastfeat2rebase
    git checkout feat2
    git commit ... # more commits here to feat2
    git rebase --onto combo lastfeat2rebase feat2
    

    Please note, that once you go with the rebase option, you cannot use the merge option anymore. If you use the merge option, you can later decide to go with the rebase option.

提交回复
热议问题