简体   繁体   English

为什么樱桃选择会导致 git 合并以丢弃我的部分提交?

[英]Why does a cherry-pick cause git merge to discard part of my commit?

So I'm trying to understand why when I git merge master into a branch, I lose some, but not all of my changes in a commit.所以我试图理解为什么当我git merge master到一个分支中时,我会丢失一些,但不是我在提交中的所有更改。 It seems to do with a cherry-pick commit on my branch.这似乎与我的分支上的樱桃选择提交有关。 Here's how to reproduce:以下是如何重现:

  1. init a repo with an initial commit初始化一个带有初始提交的 repo
  2. Add a line of text to a file and commit将一行文本添加到文件并提交
  3. Branch off this, call it "test"分支这个,称之为“测试”
  4. On branch master, revert the second commit在分支 master 上,还原第二次提交
  5. Cherry-pick that commit into the test branch Cherry-pick 提交到测试分支
  6. Go back to master, create a new commit that restores that line of text, plus add additional changes Go 回到主控,创建一个恢复那行文本的新提交,并添加其他更改
  7. Go back to test, run git merge master Go 回测试,运行git merge master
  8. Notice that the merge automatically removed that line of text (,.,), but kept the additional changes.请注意,合并自动删除了该行文本 (,.,),但保留了其他更改。 I expect the merge to not drop that line, so that it matches master .我希望合并不会删除该行,以便它匹配master

I uploaded my results on github: https://github.com/terryttsai/testRevertMerge/commits/test我在 github 上上传了我的结果: https://github.com/terryttsai/testRevertMerge/commits/test

If I never cherry-picked that commit into my test branch, then git merge master does keep that line of text.如果我从来没有将那个提交挑选到我的test分支中,那么git merge master确实保留了那行文本。 So how come when I do have that cherry-pick commit, the merge removes it?那么,当我确实有那个cherry-pick 提交时,怎么会被合并删除呢? How can I prevent this from happening in the first place?我怎样才能首先防止这种情况发生?

I expect the merge to not drop that line, so that it matches master.我希望合并不会删除该行,以便它与 master 匹配。

No. git merge master does not mean to match master!git merge master不代表匹配高手!

What is a merge?什么合并? A merge means to play out the contributions of both branches, starting from the point of divergence.合并意味着从分歧点开始发挥两个分支的贡献。 What are they?这些是什么?

Well, the point of divergence is after that first line was added.好吧,分歧点是在添加第一行之后。 So what happened after that on each branch?那么在每个分支之后发生了什么?

  • On master, the only contribution is the extra stuff.在master上,唯一的贡献是额外的东西。 (It removed the first line but later restored it, so that is not a contribution; in that respect nothing happened at all.) (它删除了第一行,但后来又恢复了它,所以这不是贡献;在这方面什么也没发生。)

  • On test, the only contribution is deletion of the first line (that is what the cherry picked revert commit does).在测试中,唯一的贡献是删除第一行(这就是樱桃选择的还原提交所做的)。

So the merge does both of them: the extra lines from master, and the deletion from test.所以合并完成了它们:master 中的额外行,以及 test 中的删除。

But if you never do the cherry pick, then neither branch makes a contribution that deletes that line, so obviously it is not deleted by the merge.但是,如果您从不进行樱桃挑选,那么两个分支都不会做出删除该行的贡献,因此显然它不会被合并删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM