为什么这个樱桃挑选有冲突? [英] Why does this cherry-pick have a conflict?

查看:101
本文介绍了为什么这个樱桃挑选有冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 git cherry-pick 是一个用于应用指定提交的更改的命令,但我认为我并不真正了解它的工作方式。



让我们说一个这样的回购行为:

  git init 

回显a> a
git add。; git commit -am'master add line a'

git checkout -b dev
echo b>> a
git commit -am'dev add line b'
echo c>> a
git commit -am'dev add line c'

git checkout master

git cherry-pick dev

我认为 cherry-pick 命令可以正常工作, code> a 转换为:

  a 

c

但实际上我收到以下信息:

 错误:无法应用08e8d3e ... dev添加行c 
提示:解决冲突后,标记更正后的路径
提示:with'git add<路径>'或'git rm<路径>'
提示:并用'git commit'提交结果

然后我运行:

  git diff 

输出:

  diff --cc a 
指数7898192,de98044..0000000
--- a / a
+++ b / a
@@@ -1,1 -1,3 +1,6 @@ b $ b a
++ << HEAD
++ =======
+ b
+ c
++>>>>>>> 11fff29 ... abc

所以我的问题是:为什么会有像git-差异显示?在这种情况下,樱桃选择工作的细节是什么?

解决方案

> 之后:

  git config merge.conflictstyle diff3 

您会得到一个更详细的区别:

 < <<<<<< HEAD 
|||||||父母5b2a14c ... dev添加行c
b
=======
b
c
>>>>>>>> ; 5b2a14c ... dev add line c

它表明,当应用由 dev 的HEAD( b c ),Git不知道共同的祖先;它遵循:


  • 樱桃选择提交的直接父项(显示它添加了一行' c '在一行之后 b ')

  • 目标提交(不显示行 b 完全可以应用添加的更改' c ')

  • ul>

    因此会发生冲突。

    樱桃采摘不像合并(它寻找一个 merge-base )。



    樱桃采摘需要提交并应用它引入的更改



    这里引入的更改为:add c b

    之上的$ c>并且目标提交完全没有 b ,所以对于Git来说:


    • 上游(目的地)提交已被删除 b (或从未有过它首先在这里是这种情况,但Git不知道这一点),
    • 源提交有一个 b 在其上添加了 c



    至于Git知道何时试图应用该补丁(并且这是所有 git cherry-pick 确实:应用补丁。它根本不会查找樱桃选择提交的历史记录),这是一个冲突:并发修改。



    如果您确信解析的方式应该如此去,你可以这样做:

     > git cherry-pick -Xtheirs dev 
    [master 7849e0c] dev add line c
    日期:Wed Aug 17 08:25:48 2016 +0200
    1个文件已更改,2个插入(+)

    然后,您会看到 b c 添加到原始提交中,没有任何冲突(因为您已经通过传递选项' -Xtheirs '指明了如何解决它到默认合并策略递归 ) p>

    I know git cherry-pick is a command that use to apply the changes of specified commit, but I think I just don't really understand the way it works.

    Let's say a repo act like that:

    git init
    
    echo a>a
    git add .; git commit -am 'master add line a'
    
    git checkout -b dev
    echo b>>a
    git commit -am 'dev add line b'
    echo c>>a
    git commit -am 'dev add line c'
    
    git checkout master
    
    git cherry-pick dev
    

    I thought cherry-pick command would work well and change file a into:

    a
    
    c
    

    but in fact I got the following message:

    error: could not apply 08e8d3e... dev add line c
    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 then I run:

    git diff
    

    output:

    diff --cc a
    index 7898192,de98044..0000000
    --- a/a
    +++ b/a
    @@@ -1,1 -1,3 +1,6 @@@
      a
    ++<<<<<<< HEAD
    ++=======
    + b
    + c
    ++>>>>>>> 11fff29... abc
    

    So my question is: Why is there a conflict like git-diff shows? What are the details of cherry-pick working in this case?

    解决方案

    Try again your cherry-pick after:

    git config merge.conflictstyle diff3
    

    You will get a more detailed diff:

    <<<<<<< HEAD
    ||||||| parent of 5b2a14c... dev add line c
    b
    =======
    b
    c
    >>>>>>> 5b2a14c... dev add line c
    

    It shows that, when applying the patch represented by dev's HEAD (b and c), Git does not know of a common ancestor; it defers to:

    • the immediate parent of the cherry-picked commit (showing that it adds a line 'c'after a line 'b')
    • the destination commit (which shows no line b at all on top of which it could apply the added change 'c')

    Hence conflict.

    Cherry-picking is not like a merge (which looks for a merge-base).

    Cherry-picking takes a commit and applies the change that it introduces.

    Here the change introduced is: add c on top of b.
    And the destination commit has no b at all, so for Git:

    • the upstream (destination) commit has "removed b" (or never had it in the first place, which is the case here, but Git does not know that),
    • the source commit has a b on top of which c is added.

    As far as Git knows when trying to apply that patch (and that is all git cherry-pick does: apply patch. It does not look for the history of the cherry-picked commit at all), that is a conflict: concurrent modification.

    If you are sure of the way that resolution should go, you can do:

    > git cherry-pick -Xtheirs dev
    [master 7849e0c] dev add line c
     Date: Wed Aug 17 08:25:48 2016 +0200
     1 file changed, 2 insertions(+)
    

    Then, you would see b and c added to the original commit, without any conflict (since you indicated how to resolve it with the option '-Xtheirs' passed to the default merge strategy recursive)

    这篇关于为什么这个樱桃挑选有冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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