需要帮助来理解合并冲突的例子

sid*_*ith 2 git merge

我正在关注一本书中的示例,该书没有显示解决合并冲突的步骤.本教程中提到的本教程对我不起作用 - 在本地系统上模拟多个用户/提交者 因此,我甚至无法学习合并.

以下是从书中复制的步骤 -

现在打开空白participants.txt文件并在其中粘贴以下行:( 我在每个名称前添加了一个连字符)

Finance team
 Charles
 Lisa
 John
 Stacy
 Alexander
Run Code Online (Sandbox Code Playgroud)

Git代码 -

git init
git add .
git commit –m 'Initial list for finance team'
Run Code Online (Sandbox Code Playgroud)

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

git checkout –b marketing
Run Code Online (Sandbox Code Playgroud)

现在打开participants.txt文件并开始输入财务团队列表下方营销部门的名称,如下所示:( 我在每个名称前添加了一个连字符)

Marketing team
 Collins
 Linda
 Patricia
 Morgan
Run Code Online (Sandbox Code Playgroud)

Git代码 -

git add .
git commit –m 'Unfinished list of marketing team'
git checkout master
Run Code Online (Sandbox Code Playgroud)

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

git add .
git commit –m "Final list from Finance team"
git checkout marketing
Run Code Online (Sandbox Code Playgroud)

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

git add .
git commit –m "Initial list of marketing team"
Run Code Online (Sandbox Code Playgroud)

说已经确认输入用于营销的相同名称; 现在我们需要合并这两个列表,这可以通过以下命令完成.

git merge master
Run Code Online (Sandbox Code Playgroud)

您将收到合并冲突,如以下屏幕截图所示.解决它们.

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

我该如何解决这些冲突?

文件中的文字如下 -

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

Marketing team
- Collins
- Linda
- Patricia
- Morgan
- Amanda
=======
>>>>>>> master
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 8

那些是合并标记:

<<<<<<<
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.
>>>>>>>
Run Code Online (Sandbox Code Playgroud)

如" 在Git中修复合并冲突? "中所述,您应该:

  • 删除它们
  • 保留要在文件的最终版本中查看的行
  • 添加和提交

或者您可以简单地签出这些文件以保留原始版本,如" 我如何丢弃远程更改并将文件标记为"已解决"? ".

如您所见,您从Finance Teamin marketing分支(StacyAlexander)中删除的名称又回来了.
所以,当你被合并mastermarketing,Git是问你:决定,我们应该保持这些名称或删除它们?


正如Charles Bailey 在评论中补充的那样,似乎缺少(基础)共同祖先部分:

|||||||
The common ancestor version.
=======
Run Code Online (Sandbox Code Playgroud)

你应该用配置重做exercice:

git config merge.conflictStyle  diff3
Run Code Online (Sandbox Code Playgroud)

这将有助于可视化三向合并的基本部分.
另请参阅" 为什么3向合并优于双向合并? ".


OP 补充道

当你决定如何保持在文本文件和删除合并标记,<<<HEAD并且>>>master,你需要将文件添加到使用阶段git add [filename],然后提交正常.
你不能git merge master马上执行.

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

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.
Run Code Online (Sandbox Code Playgroud)

这是解决方案

git add .
git commit - m "success"
git merge master
Run Code Online (Sandbox Code Playgroud)

请参阅" GIT合并错误"提交是不可能的,因为您有未合并的文件" ".