使用git merge解决冲突的单独提交 [英] A separate commit for conflict resolution with git merge

查看:567
本文介绍了使用git merge解决冲突的单独提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个大的主题分支合并到master分支中,但是我想要一个单独的提交来显示如何解决冲突.目标是使一个提交显示这些文件冲突以及它们如何冲突",而下一个提交将显示这是解决冲突的方式". IE. 第一次提交将包含冲突标记.

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". I.e. 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

但随后 git mergetool 说,尽管我的工作树中的文件具有冲突标记,但所有文件都不需要合并.

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?

我也乐于接受其他方式来获得单独提交以解决冲突"的效果.

I'm also open to other ways of getting the "separate commit for conflict resolution" effect.

推荐答案

git merge将留下冲突标记.

然后,您(通常)调用git mergetool(以您的首选项为--tool)来解决冲突.在大多数情况下,这将导致分阶段的更改. 您要更改的内容(例如,使用git reset).

You then (usually) invoke git mergetool (with the --tool of your preference) to resolve the conflicts. Most of the time, this will result in staged changes. This you want to change (e.g. using git reset).

现在隔离提交原始"合并,并随后提交git add . && git commit -m 'resolutions'git commit -am 'resolutions'来添加冲突解决方案.

Now commit the 'raw' merge in isolation, and subsequently git add . && git commit -m 'resolutions' or git commit -am 'resolutions' to add the conflict resolutions.

请注意,这会使您在合并边界修订版中处于中断"状态.

分步进行:

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

这篇关于使用git merge解决冲突的单独提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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