English 中文(简体)
解决基特冲突 合并冲突 支持在拉动中改变冲突
原标题:Resolve Git merge conflicts in favor of their changes during a pull

我如何解决大合并冲突 支持拉动的改变?

我想从工作树上去除所有相矛盾的更改, 而不必与 < code> git 合并工具 < / code > 进行所有冲突, 同时保留所有无冲突更改 。 最好是在拉动时这样做, 而不是事后这样做 。

最佳回答

如果您已经处于冲突状态,并且不想逐条检查退出路径。您可以尝试

git merge --abort
git pull -X theirs
问题回答

您可以使用循环“他们的”策略option :

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.

注意 : 如 manpage 所言, “ 我们” 合并 < 坚固 > 战略选择 < / 坚固 > 与 “ 我们” 合并 < 坚固 > 战略 < / 坚固 > 有很大不同 。

如果你已经处于冲突状态,而你只想接受他们的“全部”状态:

git checkout --theirs .
git add .

如果你想反其道而行:

git checkout --ours .
git add .

这是非常激烈的, 所以,确保你真的 想要消灭一切 像这样,在这样做之前。

所以,想象一下我刚刚在的情景:

您尝试了 < code> merge , 或者可能尝试了 < code> cherry- poker , 然后您再次停下来

$ 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

令人惊讶的是,我无法在网上轻易找到答案。

git pull - X theirs 的答案可能造成丑陋的合并承诺,或发布

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

如果您想要简单地忽略对回购中文件的任何本地修改, 例如对于总是要成为源的镜像的客户端, 请运行此项( 将 < code>master 替换为您想要的分支 ) :

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

它如何运作? < a href=" git 获取 /code pull pull ,但不合并 。然后 < a href=> https://stackoverflow.com/ quesestions/2530060/can-y-explain-what-git-reset-reset-does- in-plain-english"_code>>>_getreseet < hard

请不要让这种“ 强” 不起作用 " /强 " :

git 检出 --ours 路径/ to/ file

git 检出 - 他们的路径/ 到/ 文件

相反,我这样做了,假设“强”是属于我们的 " /强 ",而 " 强 " 是属于他们的 " /强 "

git checkout HEAD -- path/to/file

或:

git checkout MERGE_HEAD -- path/to/file

我们这样做之后,我们是善良的:

git add .

If you want to understand more, see wonderful post of torek here : git checkout --ours does not remove files from unmerged files list

< 加强> VS 代码( 集成 Git) IDE 用户:

如果您想要接受冲突文件 中的所有更改,请采取以下步骤。

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

类似地,您也可以对其它选项如 < enger > 接受全部, 接受全部当前等, 等选项进行相同的操作。

解决某一事务组中与版本的所有冲突:

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

所以,如果您已经处于合并状态, 您想要保留冲突文件的主版 :

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

2024年最新消息!

如果您想要接受所有当前修改, 忽略任何正在加入的修改, 您可以实现此目标 :

git merge [branch] --strategy-option ours

[branch] 应替换为您合并为当前分支的分支的名称 。

如果您想要覆盖任何当前更改并接受所有从新更改产生的冲突, 您可以使用 < code> 他们的 < /code > 战略替换 :

git merge [branch] --strategy-option theirs

git check out -ours/thes 并不完全解决冲突。 它从ours/thes 中检查( 获取整个文件) 。

假设我们有一个文件 < code> foo < / code >, 其中含有对两个承诺/ 树/ 树/ 任何东西的更改。 如果它们有冲突, 并且有修改, 我们想要使用 < code> ours -- 来解决冲突, 然后使用 < code> checkout -- ours foo 会丢弃引入冲突的更改, 但也放弃修改 。

Using SED

Resolve using theirs:

sed -i - i - e/ lt; <<<<<<<<</ \\ d - e/\\gt;>>>>>> > > >/d foo

  • -i Modify the file in place,
  • /^<<<<<<</,/^=======/d delete everything between and including <<<<<<< and ======= (ours)
  • /^>>>>>>>/d delete the remaining conflict marker
  • -e specify multiple patterns to SED
  • foo the file

Resolve using ours:

sed -i - i - e/ lt; <<<<< <;/ d; < <; - e/ / / gt;>>>>> > > >/d foo

我做了"https://github.com/CervEdin/gut/blob/main/git-resoluve.sh" rel=“无随从无悔者”>,你可以称之为git room -o/t/-b

Creating a custom merge tool

您可以创建自定义合并工具。 在上述 < code>sed 脚本的基础上, 您可以在您的 < code> git- config 中放置类似的东西 :

[mergetool "ours"]
    cmd = "sed -i -e  /^<<<<<<</d  -e  /^=======/,/^>>>>>>>/d  -- $MERGED"

并称之为 git 合并工具 -tool=ours

Merge styles

请注意, 还有其他非标准合并样式, 如 @ Stavros 在批注中指出的 < code> diff3 < /code > 和 zdiff3 < /code > 。 冲突标记的数量也并非硬码到 git 中的 7 。 我认为它主要是默认的, 我记得它可以配置 。

基本的 ed 脚本可以是 < a href=" https://github.com/CervEdin/gut/commit/4d784fd8c753d8ebc272a190c7ec357777e68e0a" rel=“nofollow noreferrerr">tweaked , 用于所有配置的工作。

https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging” rel=“noreferrer”>https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging

This will basically do a fake merge. It will record a new merge commit with both branches as parents, but it will not even look at the branch you’re merging in. It will simply record as the result of the merge the exact code in your current branch.

$ git merge -s ours mundo

我们的策略合并了

$ git diff HEAD HEAD~

You can see that there is no difference between the branch we were on and the result of the merge.

This can often be useful to basically trick Git into thinking that a branch is already merged when doing a merge later on. For example, say you branched off a release branch and have done some work on it that you will want to merge back into your master branch at some point. In the meantime some bugfix on master needs to be backported into your release branch. You can merge the bugfix branch into the release branch and also merge -s ours the same branch into your master branch (even though the fix is already there) so when you later merge the release branch again, there are no conflicts from the bugfix.

A situation I ve found to be useful if I want master to reflect the changes of a new topic branch. I ve noticed that -Xtheirs doesn t merge without conflicts in some circumstances... e.g.

$ 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

from topicFoo, first merge in master using the -s ours strategy, this will create the fake commit that is just the state of topicFoo. $ git merge -s ours master

检查创建的合并承诺

$ git log

现在检出母版分支

$ git checkout master

将主题分支重新合并, 但这一时间使用 - X 它们的递归策略, 这将向您提供主分支, 显示主题Foo 的状态 。

$ git merge -X theirs topicFoo

我有一个长长的ext-version 分支,其中删除了在 Develop 上更改的文件,在两个分支的不同地方添加的文件等等。

我想将 ext-version 分支的全部内容都移到 develop 中,所有内容都包含在一次合并中。

上述命令的组合对我有效是:

git merge -X theirs next-version
# lots of files left that were modified on develop but deleted on next-version
git checkout next-version .
# files removed, now add the deletions to the commit
git add .
# still have files that were added on develop; in my case they are all in web/
git rm -r web

不是新的答案,只是将许多答案的比特合并起来,部分是为了保证你可能需要这些答案的<这些>。

接受远程更改 ( theirs ), 如有冲突, 您会收到以下错误 :

fatal: Not possible to fast-forward, aborting.

以便您可以快速地拉动并接受他们的变化:

$ git pull -X theirs --ff

Starting with an example, We have a main branch and a dev branch having some conflicts

退出前先拉动更新, 因此主要分支更新 :

git checkout main
git pull
git checkout dev

然后合并 :

  • if you want to accept incoming changes from the main to be the source of truth:
git merge --strategy-option theirs main
  • if you want to accept the current changes in dev as the source of truth:
git merge --strategy-option ours main

ours thems 这是特别的单词,它们有意义,不是习惯,但 main dev 可能是您想要合并的其他两个分支。

在使用混合模式的Emacs案中,为了用地雷或地雷解决所有冲突标记,我们可以界定:

(defun aj/smerge-keep-mine-all ()
  ""
  (interactive)
  (save-excursion
    (beginning-of-buffer)
    (while (ignore-errors  user-error (progn (smerge-next) t))
      (smerge-keep-mine))))

(defun aj/smerge-keep-other-all ()
  ""
  (interactive)
  (save-excursion
    (beginning-of-buffer)
    (while (ignore-errors  user-error (progn (smerge-next) t))
      (smerge-keep-other))))

我发现我需要说明 我合并的分支的原产主产地

git merge **origin main** --strategy-option theirs

it s Soluted. 用下面的简单步骤解决所有冲突 。

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

git pull -X theirs

git pull origin master

尽管这里已经有许多答案,但我想将这一答案写成文件,说明如何在“2022年视频工作室”

如果您想要接受本问题中询问的输入的更改( their change), 请在下面的 Step 6 中以 theirs 替换 ours 。 它和此一样简单。


描述我在这里所做的事情的缩略语:

dev 合并为 my 分支 , 并同时, 如果出现合并冲突, 请从我的分支更改 。

(完成这项工作后,我将创建一个公关,以便最终将“em>my 分支” 改为dev )。


Step 1

选中您想要合并 dev 的分支所在的分支, 具体方法是到 管理分支 < /em > 并双击您的分支名称 。 就我而言, 它是 < code> fateratureI PrintBranchToDev2- AshK

Step 2:

右键点击 dev 并点击 Megel devel into protect/ I临时BranchToDev2-AshK 右键单击 /dev 右键点击 Megev dev into protic/I临时BranchToDev2-AshK

Step 3:

You ll see a ton of merge conflicts! ?

现在,右键点击每个文件并点击 keep the surrent( fature/ I临时BranchToDev2-AshK) 是非常无聊的,所以让 s Abort 来使用命令行来处理。

Step 4:

点击 Abort

Step 5:

打开 >cmd

Step 6:

git type git 合并 --strategy-option ourss -- no-commit dev 键并输入

这基本上意味着: "Mege dev 进入当前分支,如果出现合并冲突,从当前分支中取出版本,并且不进行合并。

This will be the output: Automatic merge went well; stopped before committing as requested

Step 7:

现在去视觉工作室, 冲突得到妥善处理, 你可以输入您的信息并承诺:





相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...