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

前端 未结 25 1554
慢半拍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:29

    I don't like the above approaches. Using cherry-pick is great for picking a single change, but it is a pain if you want to bring in all the changes except for some bad ones. Here is my approach.

    There is no --interactive argument you can pass to git merge.

    Here is the alternative:

    You have some changes in branch 'feature' and you want to bring some but not all of them over to 'master' in a not sloppy way (i.e. you don't want to cherry pick and commit each one)

    git checkout feature
    git checkout -b temp
    git rebase -i master
    
    # Above will drop you in an editor and pick the changes you want ala:
    pick 7266df7 First change
    pick 1b3f7df Another change
    pick 5bbf56f Last change
    
    # Rebase b44c147..5bbf56f onto b44c147
    #
    # Commands:
    # pick = use commit
    # edit = use commit, but stop for amending
    # squash = use commit, but meld into previous commit
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #
    
    git checkout master
    git pull . temp
    git branch -d temp
    

    So just wrap that in a shell script, change master into $to and change feature into $from and you are good to go:

    #!/bin/bash
    # git-interactive-merge
    from=$1
    to=$2
    git checkout $from
    git checkout -b ${from}_tmp
    git rebase -i $to
    # Above will drop you in an editor and pick the changes you want
    git checkout $to
    git pull . ${from}_tmp
    git branch -d ${from}_tmp
    
    0 讨论(0)
提交回复
热议问题