Skip to content

lab 28 Resolving Conflicts

Goals

Merge main to greet

Now go back to the greet branch and try to merge the new main.

Execute:

git checkout greet
git merge main

Output:

$ git checkout greet
Switched to branch 'greet'
$ git merge main
Auto-merging lib/hello.js
CONFLICT (content): Merge conflict in lib/hello.js
Automatic merge failed; fix conflicts and then commit the result.

If you open lib/hello.js, you will see:

lib/hello.js

<<<<<<< HEAD
const { Greeter } = require('./greeter');

// Default is World
const name = process.argv[2] || "World"

const greeter = new Greeter(name);
console.log(greeter.greet());
=======
const rl = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout,
});

module.exports.sayHello = () => {
  rl.question(`What's your name? `, (name) => {
    console.log(`Hello ${name}!`);
  });
};
>>>>>>> main


p. The first section is the version on the head of the current branch (greet). The second section is the version on the main branch.

Fix the Conflict

You need to manually resolve the conflict. Modify lib/hello.js to be the following.

lib/hello.js

const { Greeter } = require('./greeter');

const rl = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question(`What's your name`, (myName) => {
  const greeter = new Greeter(name);
  console.log(greeter.greet());

  rl.close();
});

Commit the Conflict Resolution

Execute:

git add lib/hello.js
git commit -m "Merged main fixed conflict."

Output:

$ git add lib/hello.js
$ git commit -m "Merged main fixed conflict."
[greet ca11819] Merged main fixed conflict.

Advanced Merging

git doesn’t provide any graphical merge tools, but it will gladly work with any third party merge tool you wish to use. See http://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#External-Merge-and-Diff-Tools for a description of using the Perforce merge tool with git.