5

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
Community
  • 1
  • 1
sid smith
  • 483
  • 1
  • 6
  • 16

1 Answers1

11

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

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • You might want to mention the "merge.conflictStyle = diff3" configuration option seeing as the conflict style in the question lacks the [really important] common base section. – CB Bailey Jun 08 '14 at 07:08
  • Thank again VonC. I have some questions. Could you please answer them for me ? What is this git master ? Is it the original text/code or another branch like marketing ? Thanks. – sid smith Jun 08 '14 at 07:19
  • 1
    @sidsmith master is the name of the default branch present in all git repo. See http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging or https://www.atlassian.com/git/tutorial/git-branches#!branch, but also the excellent tutorial http://pcottle.github.io/learnGitBranching/. – VonC Jun 08 '14 at 07:21
  • @sidsmith but regarding the basic concepts of git, the best introduction is at https://try.github.io/levels/1/challenges/1 – VonC Jun 08 '14 at 07:22
  • @sidsmith sure, but the interactive and visual tutorial of http://pcottle.github.io/learnGitBranching/ is quite effective too. – VonC Jun 08 '14 at 07:25
  • @CharlesBailey thank you for pointing that out. I have edited the answer accordingly. – VonC Jun 08 '14 at 07:28
  • @VonC - oh I meant pcottle link. Btw, as per old chapter in my book, the `<<< – sid smith Jun 08 '14 at 07:40
  • 1
    @sidsmith `HEAD` refers in your case to the `marketing` branch. To better understand what `HEAD` is, read the first two answer of http://stackoverflow.com/q/964876/6309. – VonC Jun 08 '14 at 07:43
  • Great, when I enter `git merge master` I get the error ` 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 ' as hint: appropriate to mark resolution and make a commit, hint: or use 'git commit -a'. fatal: Exiting because of an unresolved conflict.` – sid smith Jun 08 '14 at 07:45
  • @sidsmith sure, you should merge again if you have a merge in progress. The error message is pretty explicit on how to resolve the situation: once your files have no merge markers in them (and are added/committed), the merge is complete, and you can start a new one. – VonC Jun 08 '14 at 07:49
  • I finally fixed the error. I will add it as edit to your answer. – sid smith Jun 08 '14 at 07:54
  • Here is the solution - `git add .`, `git commit - m "success"`, `git merge master`. Link - http://stackoverflow.com/questions/12961752/git-merge-error-commit-is-not-possible-because-you-have-unmerged-files – sid smith Jun 08 '14 at 08:04
  • @sidsmith great! I'm outside right now. I will include your solution in the answer when I get back. – VonC Jun 08 '14 at 08:08