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

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

    While some of these answers are pretty good, I feel like none actually answered the OP's original constraint: selecting particular files from particular branches. This solution does that, but it may be tedious if there are many files.

    Let’s say you have the master, exp1, and exp2 branches. You want to merge one file from each of the experimental branches into master. I would do something like this:

    git checkout master
    git checkout exp1 path/to/file_a
    git checkout exp2 path/to/file_b
    
    # Save these files as a stash
    git stash
    
    # Merge stash with master
    git merge stash
    

    This will give you in-file diffs for each of the files you want. Nothing more. Nothing less. It's useful you have radically different file changes between versions --in my case, changing an application from Ruby on Rails 2 to Ruby on Rails 3.

    This will merge files, but it does a smart merge. I wasn't able to figure out how to use this method to get in-file diff information (maybe it still will for extreme differences. Annoying small things like whitespace get merged back in unless you use the -s recursive -X ignore-all-space option)

提交回复
热议问题