解决一个有利于我们或他们的git合并冲突,而无需从任何一个获取整个文件 [英] Resolving a git merge conflict in favour of ours or theirs without taking the whole file from either

查看:73
本文介绍了解决一个有利于我们或他们的git合并冲突,而无需从任何一个获取整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何解决有利于一个或另一个分支的合并冲突,有很多关于SO的问题.但是,我找到的最常见的答案是使用git checkout --oursgit checkout --theirs.

问题是git checkout将从任一分支获取 entire 文件.文件的某些部分可能合并得很好,我们不希望它们被删除.

问题是:给定处于冲突状态的文件,有没有一种方法可以删除冲突标记,而有利于我们的"或他们的" ,而无需将此策略应用于整个合并还是从我们的"或他们的"中获取整个文件?

使用简单的脚本肯定可以实现,但是我想知道是否有我找不到的现有解决方案.

解决方案

如果您想将文件中所有剩余的冲突解决到我们或他们的文件中,那么您想要的每一面和冲突样式都是简单的sed.

对于大多数代码文件而言,冲突标记是如此陌生,以至于您不必小心识别它们,只需键入即可非常简单:

sed -si '/^<<</,/^>>>/ {/^<<</,/^===/d;/^>>>/d}'  thefile  # keep theirs
sed -si '/^<<</,/^>>>/ {/^<<</d;/^===/,/^>>>/d}'  thefile  # keep ours

并做到所有事情

sed -si $yourpattern $(git ls-files -m | uniq)

最简单.

要更小心地使用冲突标记,最好制作您引用的简单脚本,并且diff3冲突报告样式也需要进行一些更改.通常,冲突符号计数为7,为确保最佳安全起见,请将sed范围设置为/^<<<<<<< ours$/,/^>>>>>>> theirs$/,并通过diff3报告保持我们的"删除范围以^||||||| base$而不是^=======$开头.

Mac上的sed已被裁定拒绝进行内联编辑而不创建备份文件,您必须执行以下操作: -si.bak,然后删除备份.

There are numerous questions on SO on how to resolve merge conflicts in favour of one or the other branch. But the most common answer I'm finding is using git checkout --ours or git checkout --theirs.

The problem is git checkout will take the entire file from either branch. There could've been parts of the file that merged just fine, and we don't want them removed.

So the question is: given a file in a conflicted state, is there a way to remove the conflict markers in favour of "ours" or "theirs" without applying this strategy to the entire merge or taking the entire file from "ours" or "theirs"?

This is definitely possible with a simple script, but I want to know if there's an existing solution I'm not finding.

解决方案

If you want to resolve all remaining conflicts in a file to ours or theirs it's a straight forward sed per side you want and your conflict style.

For most code files, where the conflict markers are so foreign that you don't have to be careful about identifying them, it's so easy you can just type it:

sed -si '/^<<</,/^>>>/ {/^<<</,/^===/d;/^>>>/d}'  thefile  # keep theirs
sed -si '/^<<</,/^>>>/ {/^<<</d;/^===/,/^>>>/d}'  thefile  # keep ours

and to do that for everything,

sed -si $yourpattern $(git ls-files -m | uniq)

is easiest.

To be more careful with your conflict markers it's better to make the simple script you referred to, and the diff3 conflict-reporting style needs some changes as well. The usual conflict symbol count is 7, for best safety make your sed range /^<<<<<<< ours$/,/^>>>>>>> theirs$/, and with diff3 reporting the "keep ours" deletion range starts with ^||||||| base$, not ^=======$.

Edit: sed on the Mac has been butchered to refuse to do inline edits without creating a backup file, you have to do e.g. -si.bak and then delete the backup.

这篇关于解决一个有利于我们或他们的git合并冲突,而无需从任何一个获取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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