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

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

    You use the cherry-pick command to get individual commits from one branch.

    If the change(s) you want are not in individual commits, then use the method shown here to split the commit into individual commits. Roughly speaking, you use git rebase -i to get the original commit to edit, then git reset HEAD^ to selectively revert changes, then git commit to commit that bit as a new commit in the history.

    There is another nice method here in Red Hat Magazine, where they use git add --patch or possibly git add --interactive which allows you to add just parts of a hunk, if you want to split different changes to an individual file (search in that page for "split").

    Having split the changes, you can now cherry-pick just the ones you want.

    0 讨论(0)
  • 2020-11-22 03:19

    I would do a

    git diff commit1..commit2 filepattern | git-apply --index && git commit

    This way you can limit the range of commits for a filepattern from a branch.

    It is stolen from Re: How to pull only a few files from one branch to another?

    0 讨论(0)
  • 2020-11-22 03:20

    There is another way to go:

    git checkout -p
    

    It is a mix between git checkout and git add -p and might quite be exactly what you are looking for:

       -p, --patch
           Interactively select hunks in the difference between the <tree-ish>
           (or the index, if unspecified) and the working tree. The chosen
           hunks are then applied in reverse to the working tree (and if a
           <tree-ish> was specified, the index).
    
           This means that you can use git checkout -p to selectively discard
           edits from your current working tree. See the “Interactive Mode”
           section of git-add(1) to learn how to operate the --patch mode.
    
    0 讨论(0)
  • 2020-11-22 03:21

    Here is how you can replace Myclass.java file in master branch with Myclass.java in feature1 branch. It will work even if Myclass.java doesn't exist on master.

    git checkout master
    git checkout feature1 Myclass.java
    

    Note this will overwrite - not merge - and ignore local changes in the master branch rather.

    0 讨论(0)
  • 2020-11-22 03:21

    When only a few files have changed between the current commits of the two branches, I manually merge the changes by going through the different files.

    git difftool <branch-1>..<branch-2>

    see also https://sites.google.com/site/icusite/setup/git-difftool

    0 讨论(0)
  • 2020-11-22 03:23

    I had the exact same problem as mentioned by you above. But I found this clearer in explaining the answer.

    Summary:

    • Check out the path(s) from the branch you want to merge,

       $ git checkout source_branch -- <paths>...
      
      Hint: It also works without `--` like seen in the linked post.
      
    • or to selectively merge hunks

       $ git checkout -p source_branch -- <paths>...
      

    Alternatively, use reset and then add with the option -p,

        $ git reset <paths>...
        $ git add -p <paths>...
    
    • Finally commit

       $ git commit -m "'Merge' these changes"
      
    0 讨论(0)
提交回复
热议问题