How to copy commits from one branch to another

后端 未结 2 1779
别跟我提以往
别跟我提以往 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:22

    There are basically two options here.

    1. You can use cherry-pick to copy the new commits on feat2 to combo.
    2. Instead of rebasing, you can merge

    Cherry Picking

    To cherry pick new commits on feat2 to combo, run the following:

    git checkout combo
    git cherry-pick <new commit> <new commit> <new commit>
    

    Merging

    Or alternately, you can merge instead of rebasing. E.g. To create combo:

    git checkout -b combo feat1
    git merge feat2
    

    Then, to "merge in new changes" from feat2, just...

    git merge feat2
    

    Likewise, to merge in new commits on feat1:

    git merge feat1
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题