首页 文章

在拉动期间解决Git合并冲突以支持其更改

提问于
浏览
826

如何解决git合并冲突,支持拉动更改?

基本上我需要从工作树中删除所有冲突的更改,而不必经历所有与 git mergetool 的冲突,同时保持所有无冲突的更改 . 最好在拉动时这样做,而不是之后 .

9 回答

  • 12

    您可以使用递归"theirs"策略选项:

    git merge --strategy-option theirs

    来自man

    ours
        This option forces conflicting hunks to be auto-resolved cleanly by 
        favoring our version. Changes from the other tree that do not 
        conflict with our side are reflected to the merge result.
    
        This should not be confused with the ours merge strategy, which does 
        not even look at what the other tree contains at all. It discards 
        everything the other tree did, declaring our history contains all that
        happened in it.
    
    theirs
        This is opposite of ours.
    

    注意:正如手册页所述,"ours"合并策略选项与"ours"合并策略非常不同 .

  • 16
    git pull -s recursive -X theirs <remoterepo or other repo>
    

    或者,简单地说,对于默认存储库:

    git pull -X theirs
    

    If you're already in conflicted state...

    git checkout --theirs path/to/file
    
  • 0

    如果你已经处于冲突状态,并且你想接受他们所有人:

    git checkout --theirs .
    git add .
    

    如果你想做相反的事情:

    git checkout --ours .
    git add .
    

    这是非常激烈的,所以在做之前确保你真的想要像这样擦除所有东西 .

  • 15

    好吧,想象一下我刚才的情景:

    你试图 merge ,或者也许是 cherry-pick ,然后你就停止了

    $ git cherry-pick 1023e24
    error: could not apply 1023e24... [Commit Message]
    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'
    

    现在,您查看冲突的文件,但您确实不想保留更改 . 在上面的例子中,文件只是我的IDE自动添加的换行符冲突了 . 要撤消更改并接受更改,最简单的方法是:

    git checkout --theirs path/to/the/conflicted_file.php
    git add path/to/the/conflicted_file.php
    

    与此相反(用您的版本覆盖传入版本)是

    git checkout --ours path/to/the/conflicted_file.php
    git add path/to/the/conflicted_file.php
    

    令人惊讶的是,我在网上很难找到这个答案 .

  • 206

    git pull -X theirs 答案可能会创建一个丑陋的合并提交,或发出一个

    错误:合并将覆盖对以下文件的本地更改:

    如果您只想忽略对repo中文件的任何本地修改,例如在应该始终是原始镜像的客户端上,请运行此命令(将 master 替换为您想要的分支):

    git fetch && git reset --hard origin/master
    

    它是如何工作的? git fetch does git pull but without merge . 然后git reset --hard使您的工作树与最后一次提交匹配 . 对repo中文件的所有本地更改都将为discarded,但新的本地文件将保持不变 .

  • 679

    要解决与特定分支中的版本的所有冲突:

    git diff --name-only --diff-filter=U | xargs git checkout ${branchName}
    

    因此,如果您已处于合并状态,并且您希望保留冲突文件的主版本:

    git diff --name-only --diff-filter=U | xargs git checkout master
    
  • 319

    请注意,有时这将是 not work

    git checkout --ours path / to / file

    要么

    git checkout - 他们的路径/到/文件

    我做了这个,假设 HEAD is oursMERGE_HEAD is theirs

    git checkout HEAD -- path/to/file
    

    要么:

    git checkout MERGE_HEAD -- path/to/file
    

    在我们这样做之后,我们很好:

    git add .
    

    如果你想了解更多,请在这里查看torek的精彩帖子:git checkout --ours does not remove files from unmerged files list

  • -1

    VS Code (integrated Git) IDE Users:

    如果要接受 all the incoming changes in the conflict file ,请执行以下步骤 .

    1. Go to command palette - Ctrl + Shift + P
    2. Select the option - Merge Conflict: Accept All Incoming
    

    同样地,你可以为 Accept All Both, Accept All Current etc., 等其他选项做

  • 769

    来自https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging这基本上会进行假合并 . 它将记录一个新的合并提交,两个分支作为父项,但它甚至不会查看您正在合并的分支 . 它将简单地记录为合并的结果,当前分支中的确切代码 . $ git merge -s我们的mundo
    由“我们的”战略合并而成 . $ git diff HEAD HEAD~
    您可以看到我们所在的分支与合并的结果之间没有区别 . 这通常很有用,基本上可以让Git认为在稍后进行合并时已经合并了一个分支 . 例如,假设您已经分支了一个发布分支,并且已经完成了一些工作,您希望在某些时候将其合并回主分支 . 与此同时,master上的一些错误修复需要被反向移植到您的发布分支中 . 你可以将bugfix分支合并到release分支中,并将我们的同一分支合并到你的master分支中(即使修复已经存在),所以当你以后再次合并release分支时,bugfix没有冲突 .

    如果我希望master能够反映新主题分支的更改,我发现这种情况很有用 . 我注意到-Xtheirs在某些情况下没有冲突就没合并......例如

    $ git merge -Xtheirs topicFoo 
    
    CONFLICT (modify/delete): js/search.js deleted in HEAD and modified in topicFoo. Version topicFoo of js/search.js left in tree.
    

    在这种情况下,我发现的解决方案是

    $ git checkout topicFoo
    

    从topicFoo开始,首先使用-s我们的策略在master中合并,这将创建一个假提交,它只是topicFoo的状态 . $ git merge -s我们的主人

    检查创建的合并提交

    $ git log
    

    现在结帐主分公司

    $ git checkout master
    

    合并主题分支,但这次使用-Xtheirs递归策略,现在将为您提供具有topicFoo状态的主分支 .

    $ git merge -X theirs topicFoo
    

相关问题