我正在按照书中的示例进行操作,该示例未显示解决 merge 冲突的步骤。这篇文章中提到的教程对我不起作用 - Simulate multiple users/committer's on local system 所以,我什至无法学习 merge 。
下面是从书上抄来的步骤-
现在打开空白的 participants.txt
文件并将以下行粘贴到其中:
(我在每个名字前加了一个连字符)
Finance team
Charles
Lisa
John
Stacy
Alexander
Git 代码-
git init
git add .
git commit -m 'Initial list for finance team'
使用以下语法创建一个名为 marketing 的新分支:
git checkout -b marketing
现在打开participants.txt
文件,开始在财务团队列表下方输入市场部的名字,如下:
(我在每个名字前加了一个连字符)
Marketing team
Collins
Linda
Patricia
Morgan
Git 代码-
git add .
git commit -m 'Unfinished list of marketing team'
git checkout master
打开文件并删除名称 Alexander 和 Stacy,保存,关闭,添加更改,然后提交 带有来自财务团队的提交消息 Final list 。
git add .
git commit -m "Final list from Finance team"
git checkout marketing
打开文件并为营销团队添加第五个名字 Amanda,保存、添加并提交。
git add .
git commit -m "Initial list of marketing team"
表示已确认为营销输入的相同名称;现在我们需要 merge 这两个列表,这可以通过以下命令完成。
git merge master
您将遇到 merge 冲突,如以下屏幕截图所示。解决它们。
Auto-merging participants.txt
CONFLICT (content): Merge conflict in participants.txt
Automatic merge failed; fix conflicts and then commit the result.
我该如何解决这些冲突?
文件中正文如下-
Finance team
-Charles
-Lisa
-John
<<<<<<< HEAD
-Stacy
-Alexander
Marketing team
- Collins
- Linda
- Patricia
- Morgan
- Amanda
=======
>>>>>>> master
最佳答案
那些是 merge 标记:
<<<<<<<
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.
>>>>>>>
如“Fix merge conflicts in Git?”中所述,您应该:
- 删除它们
- 保留你想在文件的最终版本中看到的行
- 添加并提交
或者您可以简单地 checkout 这些文件以保留原始版本,如“How can I discard remote changes and mark a file as “resolved”?”。
如您所见,您已从 Finance Team
中删除的名称在marketing
分支(Stacy
和 Alexander
)回来了。
所以当你 merge 时 master
进入marketing
,git 正在问你:决定,我们应该保留这些名称还是删除它们?
作为Charles Bailey添加 in the comments ,似乎缺少(基础)共同祖先部分:
|||||||
The common ancestor version.
=======
你应该用配置重做练习:
git config merge.conflictStyle diff3
这将有助于可视化 3-way merging 的基础部分.
另请参见“Why is a 3-way merge advantageous over a 2-way merge?”。
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 withgit add [filename]
, then commit as normal.
You cannot just executegit merge master
right away.
再次 merge 时,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.
Here is the solution
git add .
git commit - m "success"
git merge master
参见“GIT merge error “commit is not possible because you have unmerged files””。
关于git - 需要帮助来理解 merge 冲突示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103490/