发生冲突时如何强制合并成功? [英] How to force a merge to succeed when there are conflicts?

查看:28
本文介绍了发生冲突时如何强制合并成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并在一个文件中有一个冲突的拉取请求(见下文).合并pull request的说明github提供如下.执行此合并很重要,以便提交 pr 的人因此获得荣誉.

I'm trying to merge a pull request that has one conflict in one file (see below). The instructions for merging the pull request are provided by github are as follows. Its important to to perform this merge so the person submitting the pr gets credit for it.

# Step 1: From your project repository, check out a new branch and test the changes.
git checkout -b droark-master master
git pull https://github.com/droark/cryptopp.git master

# Step 2: Merge the changes and update on GitHub.
git checkout master
git merge --no-ff droark-master
git push origin master

我知道如何修复一个冲突文件中的一行.我不知道该怎么做是让 Git 执行合并并停止抱怨损坏的索引文件.

I know how to fix the one line in the one conflicting file. What I don't know how to do is make Git perform the merge and stop complaining about broken index files.

我如何让 Git 执行合并,确保提供拉取请求的人为此获得荣誉,并停止破坏索引文件?

How do I make Git perform the merge, ensure the person who provided the pull request gets credit for it, and stop breaking index files?

我尝试使用 Git 合并错误修复合并.一组错误会变成另一组错误,无穷无尽.我还尝试根据 合并期间忽略文件 并计划复制/粘贴所需的一行来重置问题文件,但索引损坏持续存在.

I tried to repair the merge with Git merge errors. One set of errors turns into another set of errors, ad infinitum. I also tried resetting the problem file according to Ignore files during merge with plans to copy/paste the one line needed, but the broken index persists.

这完全是浪费时间,我不再有兴趣尝试用 Git 的方式来做,因为它浪费了太多时间.现在我只想让 Git 执行合并并停止破坏索引文件.

This has turned into a complete waste of time, and I am no longer interested in trying to do it Git's way since it wastes so much time. Now I simply want Git to perform the merge and stop breaking index files.

这里是使用github的说明合并时产生的输出:

Here is the output produced when merging using github's instructions:

$ git pull https://github.com/droark/cryptopp.git master
From https://github.com/droark/cryptopp
 * branch            master     -> FETCH_HEAD
Auto-merging validate.h
Auto-merging validat2.cpp
Auto-merging validat1.cpp
Auto-merging test.cpp
CONFLICT (content): Merge conflict in test.cpp
Auto-merging pubkey.h
Automatic merge failed; fix conflicts and then commit the result.

推荐答案

没有解决冲突就无法合并.否则,git 怎么知道要合并什么?但是,您可以使用 git checkout --ours git checkout --theirs 从要合并的任一分支检出版本.举个例子:

There's no way to merge without resolving conflicts. Otherwise, how would git know what to merge? You can, however, checkout the version from either branch you're merging using git checkout --ours <filepath> or git checkout --theirs <filepath>. Here's an example:

假设您在合并暂存的 master 分支上:

Suppose you're on the master branch merging in staging:

git checkout master
git merge staging

和git显示一堆冲突:

And git shows a bunch of conflicts:

...
CONFLICT: Readme.md
...

如果您想保留 master 上的 Readme.md 版本,那么您可以运行:

If you want to keep the version of Readme.md that's on master, then you would run:

git checkout --ours Readme.md

请注意,由于您使用的是 master --ours 指的是这个"分支,即 master.

Note that since you're on master --ours refers to "this" branch, i.e. master.

现在,您可以简单地将其添加到索引中以将其标记为已解决:

Now, you can simply add it to the index to mark it as resolved:

git add Readme.md

这有效地忽略了 staging 分支上对 Readme.md 的任何更改.

This effectively ignores any changes to Readme.md on the staging branch.

您可以对要从合并中省略的每个文件重复此过程.完成后,像往常一样提交:

You can repeat this process for each file you want to omit from the merge. When you're done, commit as you normally would:

git commit -m "whatever..."

为了对所有有冲突的文件重复它,你可以这样做

In order to repeat it for all files with conflicts you can do

for f in $(git diff --name-only --diff-filter=U | cat); do
   echo "Resolve conflict in $f ..."
   git checkout --theirs $f
done

这篇关于发生冲突时如何强制合并成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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