代码之家  ›  专栏  ›  技术社区  ›  Hugo y

如何解决“由我们添加”的Git-Cherry-Pick冲突?

  •  1
  • Hugo y  · 技术社区  · 8 年前

    我遇到了一个问题与吉特,在冲突解决后,樱桃采摘。问题涉及对文件的修改 git mv 在以前的提交中。

    下面是要复制的完整命令的示例:

    mkdir git_repository && cd git_repository
    git init
    echo "foo" > myFooFile
    git add myFooFile 
    git commit -m "First commit add myFooFile"
    
    git checkout -b branch-a
    rm myFooFile
    echo "bar" > myBarFile
    git add -A
    git commit -m "rm myFooFile add myBarFile"
    
    git checkout master
    git mv myFooFile myBarFile
    git add -u
    git commit -m "git mv myFooFile myBarFile"
    

    现在让我们对分支A进行修改

    git cherry-pick $(git show-ref branch-a)
    >error: could not apply 70c80f3... rm myFooFile add myBarFile
    >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'
    

    这就是冲突:

    git status
    >On branch master
    >You are currently cherry-picking commit 70c80f3.
    >  (fix conflicts and run "git cherry-pick --continue")
    >  (use "git cherry-pick --abort" to cancel the cherry-pick operation)
    >
    >Unmerged paths:
    >  (use "git add <file>..." to mark resolution)
    >
    >   added by us:     myBarFile
    >
    >no changes added to commit (use "git add" and/or "git commit -a")
    

    不幸的是, git add myBarFile 然后 git cherry-pick --continue 不是解决方案,因为它创建了一个空提交。

    与…一起去 git rm myBarFile 在解析过程中不会更好,因为它会创建一个从mybarfile中删除foo的提交。

    如何妥善解决樱桃采摘冲突 由我们添加 最后我得到了我的主分支的这3个承诺?

    • rm myfoofile添加mybarfile
    • git mv myfoofile mybar文件
    • 第一次提交添加myfoofile

    我的BAR文件包含BAR?

    注:我知道我可以使用 git checkout branch-a -- myBarFile 在Git冲突解决过程中,但这不是我想要的解决方案,因为我不认为这是Git的方式。

    1 回复  |  直到 8 年前
        1
  •  1
  •   torek    8 年前

    小调:不要这样做:

    git cherry-pick $(git show-ref branch-a)
    

    git cherry-pick branch-a
    

    cherry pick命令接受标识修订,甚至是修订范围的任何内容,如中所述。 the gitrevisions documentation git show-ref 命令输出两个哈希ID git cherry-pick

    解决冲突没有唯一正确的方法。你可以用你喜欢的任何东西。这里有几个重要的事情要记住:

    • git merge 除此之外,Git没有找到实际的合并基,而是使用您挑选的提交的父级作为合并基(当然,最终提交是常规的非合并提交)。

    • git diff 命令:

      git diff --find-renames <merge-base> HEAD     # figure out what we did
      git diff --find-renames <merge-base> <other>  # figure out what they did
      

      git status 命令将这些显示为“未合并”,但您可以使用 git ls-files --stage

    • 此时,您的工作只是在索引中排列您希望在最终提交时由于合并(在本例中是cherry-pick)而生成的文件。这些文件需要在第0阶段,也就是索引中正常的、没有冲突的文件所在的阶段。阶段1中的任何条目表示合并基中文件的版本,阶段2中的任何条目表示 HEAD

    冲突本身

    git diff --find-renames branch-a .因此,其母公司 branch-a^

    $ git diff --find-renames branch-a^ HEAD
    diff --git a/myFooFile b/myBarFile
    similarity index 100%
    rename from myFooFile
    rename to myBarFile
    

    $ git diff --find-renames branch-a^ branch-a
    diff --git a/myBarFile b/myBarFile
    new file mode 100644
    index 0000000..5716ca5
    --- /dev/null
    +++ b/myBarFile
    @@ -0,0 +1 @@
    +bar
    diff --git a/myFooFile b/myFooFile
    deleted file mode 100644
    index 257cc56..0000000
    --- a/myFooFile
    +++ /dev/null
    @@ -1 +0,0 @@
    -foo
    

    myFooFile myBarFile foo 创建时 bar

    它不可能两者兼而有之,所以我们会有冲突。同时它 可以 做我们做过的重命名,所以它在工作树中做过(仅此),离开 MyBar文件 包含 FOO公司 .

    你想知道:

    如何妥善解决我们加入的樱桃采摘冲突,最终…我的BAR文件包含BAR?

    所有的Git需要的是你写,到工作树,一个版本的 MyBar文件 包含所需的内容,然后调整索引,使其条目处于阶段0。目前,索引中的内容是:

    $ git ls-files --stage
    100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 2      myBarFile
    

    和 MyBar文件 有你的内容

    git checkout branch-a -- myBarFile 在Git冲突解决过程中…

    一 git show 提取 文件版本:

    $ git show branch-a:myBarFile > tmp
    $ cat tmp
    bar
    $ mv tmp myBarFile
    $ git add myBarFile
    

    $ git ls-files --stage
    100644 5716ca5987cbf97d6bb54920bea6adde242d87e6 0       myBarFile
    

    git cherry-pick --continue git commit 要完成这个樱桃采摘:

    $ git cherry-pick --continue
    

    rm myFooFile add myBarFile

    [master 1837c17] rm myFooFile add myBarFile                   
     Date: Fri Jun 15 22:21:48 2018 -0700
     1 file changed, 1 insertion(+), 1 deletion(-)
    

    git log --oneline 显示所需内容:

    1837c17 (HEAD -> master) rm myFooFile add myBarFile
    a30b874 git mv myFooFile myBarFile
    bde7df5 First commit add myFooFile
    

    log.decorate = auto 在我的配置中)。