How can I selectively merge or pick changes from another branch in Git?

前端 未结 25 1600
慢半拍i
慢半拍i 2020-11-22 02:53

I\'m using Git on a new project that has two parallel -- but currently experimental -- development branches:

  • master: import of existing codebase pl
25条回答
  •  醉酒成梦
    2020-11-22 03:16

    I like the previous 'git-interactive-merge' answer, but there's one easier. Let Git do this for you using a rebase combination of interactive and onto:

          A---C1---o---C2---o---o feature
         /
    ----o---o---o---o master
    

    So the case is you want C1 and C2 from the 'feature' branch (branch point 'A'), but none of the rest for now.

    # git branch temp feature
    # git checkout master
    # git rebase -i --onto HEAD A temp
    

    Which, as in the previous answer, drops you in to the interactive editor where you select the 'pick' lines for C1 and C2 (as above). Save and quit, and then it will proceed with the rebase and give you branch 'temp' and also HEAD at master + C1 + C2:

          A---C1---o---C2---o---o feature
         /
    ----o---o---o---o-master--C1---C2 [HEAD, temp]
    

    Then you can just update master to HEAD and delete the temp branch and you're good to go:

    # git branch -f master HEAD
    # git branch -d temp
    

提交回复
热议问题