Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to understand merge conflict example

Tags:

git

merge

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
like image 594
sid smith Avatar asked Jun 08 '14 06:06

sid smith


1 Answers

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”".

like image 81
VonC Avatar answered Oct 15 '22 22:10

VonC