停止cherry-picking,开始合并,第8部分:合并部分cherry-pick

109 阅读1分钟

停止cherry-picking,开始合并,第8部分:合并部分cherry-pick

原文作者:Raymond Chen,发布于2018年3月21日

在上一篇文章中,我们看到了如何防止更改通过合并传播到其他分支。但是,有时候我们可能会遇到这样的情况:我们想要合并feature分支上的部分更改,而不是全部更改。

在这种情况下,我们可以使用git cherry-pick-x选项来记录cherry-pick的来源,然后使用git merge--strategy-option=ours--strategy-option=theirs选项来选择要保留的更改。

让我们看看这是如何工作的。假设你有以下提交历史:

%%{init: { 'gitGraph': {'showBranches': true, 'showCommitLabel':true, 'mainBranchName': 'master'}} }%%
gitGraph
   commit id: "A"
   branch feature
   checkout feature
   commit id: "F1"
   commit id: "F2"
   commit id: "F3"
   checkout master
   commit id: "M1"
   commit id: "M2"

其中F2和F3包含我们想要合并的更改,但是F3还包含一些我们不想合并的更改。首先,我们使用git cherry-pick -x F2来cherry-pick F2,这会创建一个新的提交M3,它包含F2的更改,并且会记录F2作为cherry-pick的来源。

%%{init: { 'gitGraph': {'showBranches': true, 'showCommitLabel':true, 'mainBranchName': 'master'}} }%%
gitGraph
   commit id: "A"
   branch feature
   checkout feature
   commit id: "F1"
   commit id: "F2"
   commit id: "F3"
   checkout master
   commit id: "M1"
   commit id: "M2"
   commit id: "M3" type: REVERSE

然后,我们使用git merge -s recursive -X ours feature来合并feature分支,但是选择保留master分支上的更改。这样,git会采用M3的更改,而忽略F3的更改。

这样,我们就成功地合并了feature分支上的部分更改,而忽略了其他更改。这种方法适用于任何需要选择性合并的情况。

导航