停止cherry-picking,开始合并,第7部分:防止更改离开分支

56 阅读1分钟

停止cherry-picking,开始合并,第7部分:防止更改离开分支

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

在上一篇文章中,我们看到了如何用永久修复替换临时修复。但是,有时候我们可能会遇到这样的情况:我们想要确保某些更改只在特定的分支上,而不会通过合并传播到其他分支。

在这种情况下,我们可以使用git merge--no-ff选项来防止快进合并。快进合并是git的默认合并行为,它会直接将当前分支指向目标分支的最新提交,而不会创建一个新的合并提交。

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

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

其中F2包含一些我们不想传播到master分支的更改。如果我们使用git merge feature,git会执行快进合并,直接将master指向F2,这样F2的更改就会传播到master分支。

但是,如果我们使用git merge --no-ff feature,git会创建一个新的合并提交M3,它包含F2的更改,但是不会将master指向F2。

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

这样,F2的更改就不会通过合并传播到master分支,因为master分支指向的是M3,而不是F2。这种方法适用于任何需要防止更改通过合并传播的情况。

导航