简体   繁体   English

使用git merge单独提交冲突解决方案

[英]A separate commit for conflict resolution with git merge

I'm trying to merge a big topic branch into the master, but I want a separate commit that shows how the conflict resolution happened. 我正在尝试将一个大主题分支合并到主服务器中,但我想要一个单独的提交来显示冲突解决方案是如何发生的。 The goal is to have one commit that shows "these files conflicted and how they conflicted" and the next commit would show "this is how the conflicts were resolved". 目标是让一个提交显示“这些文件冲突以及它们如何冲突”,下一个提交将显示“这就是解决冲突的方式”。 Ie the first commit would contain the conflict markers . 第一次提交将包含冲突标记

The reason for this is that the big topic branch has been reviewed and tested, as has the master branch. 原因是大主题分支已经过审查和测试,主分支也是如此。 Of the merge, we want to review only the parts that needed some work (conflicts and other merge work). 在合并中,我们只想查看需要一些工作的部分(冲突和其他合并工作)。

Here's what I'm doing this far: 这是我到目前为止所做的:

git checkout master
git checkout -b merge-from-topic
git merge topic

To record the files that have conflicts, I use a temporary file: 要记录有冲突的文件,我使用临时文件:

git diff --name-only --diff-filter=U >conflicts.txt

First I simply add those files, with the conflict markers, to a commit: 首先,我只是将带有冲突标记的文件添加到提交中:

xargs git add <conflicts.txt
git commit

Then I create another branch (for review purposes) in which I'd like to do the conflict resolution: 然后我创建另一个分支(用于审查目的),我想在其中进行冲突解决:

git checkout -b resolve-merge-from-topic

To restore the conflicts, I tried 为了恢复冲突,我试过了

xargs git reset HEAD^ -- <conflicts.txt

but then git mergetool said that none of the files need merging although files in my working tree had conflict markers. 但是后来git mergetool说没有文件需要合并,尽管我工作树中的文件有冲突标记。

How do I restore files listed in conflicts.txt, so that I can use git mergetool on them? 如何恢复conflict.txt中列出的文件,以便我可以在它们上使用git mergetool

I'm also open to other ways of getting the "separate commit for conflict resolution" effect. 我也对其他获得“单独提交冲突解决”效果的方式持开放态度。

git merge will leave conflict markers. git merge将留下冲突标记。

You then (usually) invoke git mergetool (with the --tool of your preference) to resolve the conflicts. 然后(通常)调用git mergetool (使用您喜欢的--tool )来解决冲突。 Most of the time, this will result in staged changes . 大多数情况下,这将导致分阶段的变化 This you want to change (eg using git reset ). 这是你想要改变的 (例如使用git reset )。

Now commit the 'raw' merge in isolation, and subsequently git add . && git commit -m 'resolutions' 现在单独提交'raw'合并,然后git add . && git commit -m 'resolutions' git add . && git commit -m 'resolutions' or git commit -am 'resolutions' to add the conflict resolutions. git add . && git commit -m 'resolutions' git commit -am 'resolutions' git add . && git commit -m 'resolutions'git commit -am 'resolutions'来添加冲突解决方案。

Note this leaves you with a 'broken' build at the merge boundary revision. 请注意,这会在合并边界修订版中留下“破损”构建。

In steps: 步骤:

git checkout -b be_merged       # start a temp branch for the merge (safety)

git merge --no-commit diverge   # initiate merge from a diverged branch
git status                      # shows conflicts
git mergetool                   # resolve them as always
git status                      # shows resolutions as staged
git reset                       # unstage the changes
git status                      # shows unstaged changes (good!)
git commit -m 'merge, conflicts pending'   # commit the merge
git commit -am 'conflicts resolved'        # commit the resolutions

if you are really insistent on getting the merge history, then your best bet is to use git-imerge 如果你真的坚持要获得合并历史,那么最好的办法是使用git-imerge

AFAIK the git imerge gives you a choice if the merging history should be preserved or not AFAIK git imerge可以让您选择是否应保留合并历史记录

REF: REF:

  1. http://softwareswirl.blogspot.de/2013/05/git-imerge-practical-introduction.html http://softwareswirl.blogspot.de/2013/05/git-imerge-practical-introduction.html

an exert from the author's blog: 作者博客的一篇文章:

git merge pain git merge疼痛

  • You need to resolve one big conflict that mixes up a lot of little changes on both sides of the merge. 你需要解决一个大的冲突,它会在合并的两边混合很多小的变化。 (Resolving big conflicts is hard!) (解决大冲突很难!)
  • Merging is all-or-nothing: 合并是全有或全无:
    1. There is no way to save a partly-done merge, so 没有办法保存部分完成的合并,所以
      • You can't record your progress. 您无法记录进度。
      • You can't switch to another branch temporarily. 您无法暂时切换到另一个分支。
      • If you make a mistake, you cannot revert only part of the merge. 如果您犯了错误,则无法仅还原部分合并。
      • If you cannot resolve the whole conflict, there is nothing to do but start over. 如果你无法解决整个冲突,那么除了重新开始之外没有什么可做的。
    2. There is no way to test a partly-done merge--the code won't even build until the conflict is completely resolved. 没有办法测试部分完成的合并 - 在冲突完全解决之前,代码甚至不会构建。
  • It is difficult to collaborate on a merge with a colleague. 与同事合并很难合作。

which may be exactly why you require a conflict-commit in the first place. 这可能正是您首先需要冲突提交的原因。


On another hand, a different perspective: 另一方面,不同的观点:

The merging changes should be kept as minimal as possible on an trivial project Why this is being said is that a merge touching all of the files in the repository will make you have a real pain in the future. 在一个简单的项目中,合并更改应尽可能保持最小化为什么要说的是,触及存储库中所有文件的合并将使您在将来真正感到痛苦。 As if you are to merge your branch with some major branch , lets say you work on the production branch, while the releases are done from the release branch, a branch maintainer will just try to rebase your branch with his branch. 好像你要将你的分支与一些主要分支合并,假设你在生产分支上工作,而发布是从发布分支完成的,分支维护者只会尝试用他的分支重新分支你的分支。

Now if you had a commit which touched all the files in the place, this is going to be a read bad stuff as the merge will most likely to have conflict (major one, probably the conflict-commit you made) so the maintainer will have 2 options: 现在,如果你有一个触及该地方所有文件的提交,这将是一个阅读不好的东西,因为合并最有可能会发生冲突(主要的一个,可能是你所做的冲突提交)所以维护者会有2个选项:

  1. merge it himself/herself 自己合并
  2. reject the merge and let you do the dirty work 拒绝合并,让你做脏工作

its more likely that maintainer will be opting the latter as it's the easier one for him. 维护者更有可能选择后者,因为对他来说这更容易。

So this you'll be spending more time in resolving the conflicts than doing some productive work. 因此,您将花费更多时间来解决冲突,而不是做一些富有成效的工作。

NOTE: 注意:

The second perspective is only applicable when you have feature topics that have simple span. 第二个视角仅适用于具有简单跨度的要素主题。 When merging the feature topics having larger span, it's better to use imerge and keep the history, that way you'll have smaller commits and rebase will not be painful. 合并具有更大跨度的特征主题时,最好使用imerge并保留历史记录,这样您将拥有较小的提交,并且rebase不会很痛苦。

this is a very bad idea. 这是一个非常糟糕的主意。 You want a stable code base at all times. 您希望始终拥有稳定的代码库。 Why would you ever push a code in a state where its conflicted. 为什么你会在一个冲突的状态下推送代码。

If you want to see how conflict was resolved, do a diff on the file and see the history of it. 如果要查看冲突是如何解决的,请对文件执行diff操作并查看其历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM