Git合并特定commits 到另一个分支

https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/

http://blog.csdn.net/ybdesire/article/details/42145597

经常被问到如何从一个分支合并特定的commits到另一个分支。有时候你需要这样做,只合并你需要的那些commits,不需要的commits就不合并进去了。

  

  • 合并某个分支上的单个commit

首先,用git log或GitX工具查看一下你想选择哪些commits进行合并,例如:

dd2e86 - 946992 -9143a9 - a6fd86 - 5a6057 [master]

           \

            76cada - 62ecb3 - b886a0 [feature]

比如,feature 分支上的commit 62ecb3 非常重要,它含有一个bug的修改,或其他人想访问的内容。无论什么原因,你现在只需要将62ecb3 合并到master,而不合并feature上的其他commits,所以我们用git cherry-pick命令来做:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. git checkout master  
  2. git cherry-pick 62ecb3  

这样就好啦。现在62ecb3 就被合并到master分支,并在master中添加了commit(作为一个新的commit)。cherry-pick 和merge比较类似,如果git不能合并代码改动(比如遇到合并冲突),git需要你自己来解决冲突并手动添加commit。

 

  • 合并某个分支上的一系列commits

 

在一些特性情况下,合并单个commit并不够,你需要合并一系列相连的commits。这种情况下就不要选择cherry-pick了,rebase 更适合。还以上例为例,假设你需要合并feature分支的commit76cada ~62ecb3 到master分支。

首先需要基于feature创建一个新的分支,并指明新分支的最后一个commit:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. git checkout -bnewbranch 62ecb3  

然后,rebase这个新分支的commit到master(--ontomaster)。76cada^ 指明你想从哪个特定的commit开始。

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. git rebase --ontomaster 76cada^  

得到的结果就是feature分支的commit 76cada ~62ecb3 都被合并到了master分支。

 

 

I’m often asked how to merge only specific commits from another branch into the current one. The reason you’d want to do this is to merge specific changes you need now, leaving other code changes you’re not interested in right now behind.

First of all, use git log or the awesome GitX tool to see exactly which commit you want to pick. An example:

dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
           \
            76cada - 62ecb3 - b886a0 [feature]

Let’s say you’ve written some code in commit 62ecb3 of the feature branch that is very important right now. It may contain a bug fix or code that other people need to have access to now. Whatever the reason, you want to have commit 62ecb3 in the master branch right now, but not the other code you’ve written in the feature branch. ~ Here comes git cherry-pick. In this case, 62ecb3 is the cherry and you want to pick it!

git checkout master
git cherry-pick 62ecb3

That’s all. 62ecb3 is now applied to the master branch and commited (as a new commit) in mastercherry-pick behaves just like merge. If git can’t apply the changes (e.g. you get merge conflicts), git leaves you to resolve the conflicts manually and make the commit yourself.

Cherry picking a range of commits

In some cases picking one single commit is not enough. You need, let’s say three consecutive commits. cherry-pick is not the right tool for this. rebase is. From the previous example, you’d want commit 76cada and 62ecb3 in master.

The flow is to first create a new branch from feature at the last commit you want, in this case 62ecb3.

git checkout -b newbranch 62ecb3

Next up, you rebase the newbranch commit --onto master. The 76cada^ indicates that you want to start from that specific commit.

git rebase --onto master 76cada^

The result is that commits 76cada through 62ecb3 are applied to master.

posted on 2015-09-17 11:41  阿笨猫  阅读(1348)  评论(0编辑  收藏  举报