Git樱桃选择会导致合并冲突,而合并则不会 [英] Git cherry-pick causes merge conflict while merging does not

查看:274
本文介绍了Git樱桃选择会导致合并冲突,而合并则不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习如何使用git cherry pick,我通过执行 git cherry-pick来读取 git 返回的手册页。帮助但这似乎没有帮助。我会尝试解释下面的问题。我有两个分支 master 其他

分支 master
提交历史

  0x2主分支的第二次提交
0x1初始提交

存储库中唯一跟踪 readme 的文件包含以下内容:

 某些文本

开分支其他
提交历史记录为

  0x5 CHECKPOINT,转到master 
0x4第二次提交从分支其他
0x3第一次提交从其他分支:
0x2第二次提交从主分支
0x1初始提交

readme 文件的内容为

 一些文字。 

添加其他分支的一些文本。添加更多文字。

去挑选这个提交。

工作目录在两个分支上都是干净的,没有未跟踪的更改。从这一点上,当我切换到主分支并与合并其他时,合并恰巧发生,没有合并冲突。但是当我尝试 git cherry-pick 0x5 时,会出现合并冲突,我从 git

 错误:无法应用0x5 ... CHECKPOINT,将樱桃挑到master 
提示:解决冲突后,用'git add >'或'git rm<路径>'
提示修改后的路径
hint:并提交结果'git commit'

readme 文件包含以下内容

 <<<<<<< HEAD 
某些文本
=======
某些文本。

添加其他分支的一些文本。添加更多文字。

去挑选这个提交。
>>>>>>> 0x5 ... CHECKPOINT,去挑选master

为什么会出现这种合并冲突?我试图理解它为什么会发生。不应> cherry-pick ing与尝试对提交进行的所有编辑相同 cherry-pick ed自己,然后将这个改变提交到分支(在这种情况下 master )?



另外在 git 中是否存在合并冲突?我似乎在奇怪的时候得到他们。这个实现是否依赖于(例如,依赖于用于合并的算法)?



谢谢!

解决方案

阅读:



如果第一行(某些文本。而不是某些文本)的修改立即完成0x2,就像在0x3中一样,然后 git cherry-pick 0x3 不会发生冲突。

稍后做了一些提交, git cherry-pick 不会生成与0x2内容兼容的补丁。



在这里没有共同祖先的概念(错误地在这里说明):只有补丁。

git cherry-pick ,即使它使用合并策略,不是一个 git merge


  • 一个 git merge 会寻找一个共同的祖先(回溯到历史中0x5的提交,找到一个常见的提交0x2:这里是0x2本身)

  • 一个 git cherry-pick 只会创建一个补丁通过在两个提交(默认情况下,您提到的提交及其直接父代0x4)之间进行统一的差异来确定。


关键是:补丁将无法应用在0x2之上。



试试看你自己:a git cherry-pick 0x5 就像做一个 git diff -p 0x4..0x5

你会看到中的不是是0x2的一部分,并反映来自0x3或0x4的变化。



这种补丁不能应用于0x2,因为它基于内容存在为0x2。



我创建了一个测试回购,其中我通过主人进行了两项修改。




  • 从其他分支添加一些文本(不需要修改t他的第一行)

  • 然后再修改第一行。



补丁看起来像:

  C:\ Users \ vonc \prog\git\tests\cp> git diff -p dev〜..dev 
diff --git a / fb / f
index 2b6dd7b..1c4bb79 100644
--- a / f
+++ b / f
@@ -1,4 +1,4 @@
- 某些文字
+某些文字。^ M

添加一些文字其他分支。添加更多文字。

我无法应用(樱桃选择)提交到0x2,因为0x2没有行从其他分支添加一些文本还没有。它只有一行一些文本



这就是为什么git报告冲突的原因:内容分歧,并且该修补程序应该将包含添加一些来自其他分支的文本的文件顶部的第1行更改,其中0x2不具有。



这当然不同于 git merge ,因为git merge会寻找一个共同的祖先(这里是0x2),并报告所有修改0x2到0x2没有任何问题。


I am trying to learn how to use git cherry pick, I read the manual pages that git returns by doing git cherry-pick --help but that did not seem to help. I will try and explain the problem below. I have two branches master and other.

On branch master The commit history is

0x2 Second commit from master branch
0x1 Initial commit

And the only file in the repository that I am tracking readme has the following contents

Some text

On branch other The commit history is

0x5 CHECKPOINT, going to cherry-pick onto master
0x4 second commit from branch other
0x3 first commit from other branch:
0x2 Second commit from master branch
0x1 Initial commit

And the contents of the readme file are

Some text.

Adding some text from other branch.  Adding some more text.

Going to cherry-pick this commit.

The working directory is clean on both branches with no untracked changes. From this point on when I switch to the master branch and merge with git merge other the merge happens gracefully with no merge conflicts. But when I try git cherry-pick 0x5 there is a merge conflict, I get the following output from git

error: could not apply 0x5... CHECKPOINT, going to cherry-pick onto master
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

And the readme file has the following contents

<<<<<<< HEAD
Some text
=======
Some text.

Adding some text from other branch.  Adding some more text.

Going to cherry-pick this commit.
>>>>>>> 0x5... CHECKPOINT, going to cherry-pick onto master

Why is there this merge conflict? I am trying to understand why it occurs. Shouldn't cherry-picking be the same as trying to make all the edits made on the commit that is to be cherry-picked yourself and then commiting that change onto the branch (master in this case)?

Also when exactly is there a merge conflict in git? I seem to get them at weird times. Is this implementation dependent (for example dependent on the algorithm used to merge)?

Thank you!

解决方案

I just tested that scenario, after reading:

If the modification to the first line (Some text. instead of Some text) was done immediately after 0x2, as in 0x3, then git cherry-pick 0x3 works without conflict.

But if that same change is done a few commits later, the git cherry-pick won't generate a patch compatible with the content of 0x2.

There is no notion of "common ancestor" here (as incorrectly stated here): only of "patch".

A git cherry-pick, even if it uses merging strategies, is not a git merge.

  • A git merge will look for a common ancestor (going back in the history of commits of 0x5, to find a common commit with 0x2: here 0x2 itself)
  • A git cherry-pick only creates a patch by doing a unified diff between two commits (by default, the commit you mention and its immediate parent 0x4)

The point is: that patch won't be able to be applied on top of 0x2.

Try if for yourself: a git cherry-pick 0x5 is like doing a git diff -p 0x4..0x5.
You will see in that patch lines which are not part of 0x2, and reflect changes coming from 0x3 or 0x4.

Such a patch cannot be applied on 0x2, as it is based on a content which does not exist for 0x2.

I have created a test repo in which I made from master two modifications.

  • Adding some text from other branch (no modification to the first line)
  • Then a second commit modifying the first line.

Here what my patch looks like:

C:\Users\vonc\prog\git\tests\cp>git diff -p dev~..dev
diff --git a/f b/f
index 2b6dd7b..1c4bb79 100644
--- a/f
+++ b/f
@@ -1,4 +1,4 @@
-"Some text"
+"Some text."^M

 Adding some text from other branch.  Adding some more text.

I cannot apply (cherry-pick) that commit onto 0x2, as 0x2 has no line "Adding some text from other branch" yet. It only has one line "Some text".

That is why git reports a conflict: the content has diverged, and that patch is supposed to change line 1 on top of a file including "Adding some text from other branch", which 0x2 does not have.

This differs from a git merge of course, as a git merge will look for a common ancestor (here 0x2) and reports all modifications since 0x2 onto 0x2 without any issue.

这篇关于Git樱桃选择会导致合并冲突,而合并则不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆