需要帮助来理解合并冲突的例子 [英] Need help to understand merge conflict example

查看:141
本文介绍了需要帮助来理解合并冲突的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一本没有显示解决合并冲突步骤的书中找到一个例子。这篇教程告诉我,在这篇文章中提到的不适合我 - 模拟多重用户/提交者在本地系统上
因此,我甚至无法学习合并。



以下是从本书复制的步骤 - 现在打开空白 participants.txt 文件,并在其中粘贴以下行:
我在每个名字前加了一个连字符

 财务团队
Charles
Lisa
John
Stacy
Alexander

Git代码 -

  git init 
git add。
git commit -m'财务团队的初始列表'

创建一个名为marketing的新分支使用以下语法:

  git checkout -b marketing 
participants.txt 文件,并开始为财务团队列表下方的市场部门输入名称,如下所示:


我在每个名称前加了一个连字符

 营销团队
Collins
Linda
Patricia
Morgan

Git代码 -

  git add。 
git commit -m'未完成的营销团队列表'
git checkout master

打开文件并删除名称Alexander和Stacy,保存,关闭,添加更改,并使用提交消息从财务团队的最终列表中提交

  git add。 
git commit -m财务团队的最终名单
git checkout marketing

打开文件并为营销团队添加第五个名称Amanda,保存,添加和提交。

  git add 。 
git commit -m营销团队的初始列表

说出输入的同名营销已得到证实;现在我们需要合并这两个列表,这可以通过以下命令完成。

  git merge master 

您将收到合并冲突,如以下屏幕截图所示。

 自动合并参与者.txt 
CONFLICT(内容):合并参与者.txt中的冲突
自动合并失败;修复冲突,然后提交结果。

如何解决这些冲突?

b

文件中的文字如下 -

 财务团队
- Charles
-Lisa
-John
<< >> <<< < HEAD
-Stacy
-Alexander

营销团队
- 柯林斯
- 琳达
- 帕特里夏
- 摩根
- Amanda
=======
>>>>>>> master


解决方案

这些是合并标记:

  << <<<<<< 
在合并到的分支上所做的更改。在大多数情况下,
这是我目前检出的分支(即HEAD)。
|||||||
共同的祖先版本。
=======
在合并的分支上所做的更改。这通常是
特性/主题分支。
>>>>>>>

解决Git合并冲突?,你应该:


  • 删除它们

  • 保留要在文件的最终版本中看到的行

  • 添加和提交



或者您可以简单地检出这些文件以保留原始版本,如如何放弃远程更改并标记

正如您所看到的,您从 Finance Team 中删除​​的名称 code>营销分行( Stacy Alexander )返回。
所以当你将 master 合并到 marketing 时,git会问你:决定,我们应该保留这些名称还是删除它们?






由于 Charles Bailey 增加了在评论中,似乎是(基地)共同的祖先节丢失:

  ||||||| 
共同的祖先版本。
=======

您应该使用config重做练习:

  git config merge.conflictStyle diff3 

这将有助于可视化三方合并

另请参阅为什么三方合并优于2-way merge?






OP 增加


决定在文本文件中保留的内容并删除合并标记<<< HEAD< / code>和< code>>>> master< / code>,您需要添加文件到 git添加[文件名] ,然后按照正常提交。

你不能只执行 git merge master 马上。


再次合并时,OP会报告错误消息:

 错误:'合并'是不可能的,因为你有没有合并的文件。 
提示:修正它们在工作树中,
hint:然后使用'git add / rm< file>'作为
提示:适当地标记解析并进行提交,
提示:或使用'git commit -a'。

致命:由于未解决的冲突而退出。




这是解决方案
blockquote>

  git add。 
git commit -msuccess
git merge master

请参阅< git合并错误提交是不可能的,因为你有未合并的文件。


I am following an example from a book which does not show the step to resolve a merge conflict. The tutorial which teaches that did not work for me as mentioned in this post - Simulate multiple users/committer's on local system So, I could not even learn the merge.

Here are the steps copied from the book -

Now open the blank participants.txt file and paste the following lines in it: (I added a hyphen before each name)

Finance team
 Charles
 Lisa
 John
 Stacy
 Alexander

Git code -

git init
git add .
git commit –m 'Initial list for finance team'

Create a new branch called marketing using the following syntax:

git checkout –b marketing

Now open the participants.txt file and start entering the names for the marketing department below the finance team list, as follows: (I added a hyphen before each name)

Marketing team
 Collins
 Linda
 Patricia
 Morgan

Git code -

git add .
git commit –m 'Unfinished list of marketing team'
git checkout master

Open the file and delete the names Alexander and Stacy, save, close, add the changes, and commit with the commit message Final list from Finance team .

git add .
git commit –m "Final list from Finance team"
git checkout marketing

Open the file and add the fifth name, Amanda, for the marketing team, save, add, and commit.

git add .
git commit –m "Initial list of marketing team"

Say the same names entered for marketing have been confirmed; now we need to merge these two lists, which can be done by the following command.

git merge master

You will get a merge conflict as shown in the following screenshot. Resolve them.

Auto-merging participants.txt
CONFLICT (content): Merge conflict in participants.txt
Automatic merge failed; fix conflicts and then commit the result.

How do I resolve those conflicts ?

The text in the file is as follows -

Finance team
-Charles
-Lisa
-John
<<<<<<< HEAD
-Stacy
-Alexander

Marketing team
- Collins
- Linda
- Patricia
- Morgan
- Amanda
=======
>>>>>>> master

解决方案

Those are merge markers:

<<<<<<<
Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
|||||||
The common ancestor version.
=======
Changes made on the branch that is being merged in. This is often a 
feature/topic branch.
>>>>>>>

As explained in "Fix merge conflicts in Git?", you are supposed to:

  • remove them
  • keep the lines you want to see in the final version of the file
  • add and commit

Or you can simply checkout those file to keep the original version, as in "How can I discard remote changes and mark a file as "resolved"?".

As you can see, the name you have deleted from the Finance Team in marketing branch (Stacy and Alexander) are back.
So when you are merging master into marketing, git is asking you: decide, should we keep those names or removes them?


As Charles Bailey adds in the comments, it seems the (base) common ancestor section is missing:

|||||||
The common ancestor version.
=======

you should redo the exercice with the config:

git config merge.conflictStyle  diff3

That will help visualize the base section of the 3-way merging.
See also "Why is a 3-way merge advantageous over a 2-way merge?".


The OP adds

After you decide what to keep in the text file and remove the merge markers, <<<HEAD and >>>master, you need to add the files to the stage with git add [filename], then commit as normal.
You cannot just execute git merge master right away.

When merging again, the OP reports the error message:

error: 'merge' is not possible because you have unmerged files. 
hint: Fix them up in the work tree, 
hint: and then use 'git add/rm <file>' as 
hint: appropriate to mark resolution and make a commit, 
hint: or use 'git commit -a'. 

fatal: Exiting because of an unresolved conflict.

Here is the solution

git add .
git commit - m "success"
git merge master

See "GIT merge error "commit is not possible because you have unmerged files"".

这篇关于需要帮助来理解合并冲突的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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