Skip to content

lab 27 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.py
CONFLICT (content): Merge conflict in lib/hello.py
Automatic merge failed; fix conflicts and then commit the result.

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

lib/hello.py

<<<<<<< HEAD
import sys

from greeter import Greeter

name = sys.argv[0]
greeter = Greeter(name)
greeter.greet()
=======
print("What's your name?")
name = input().strip()

print(f"Hello, {name}!")
>>>>>>> main

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.py to be the following.

lib/hello.py

from greeter import Greeter

print("What's your name?")
name = input().strip()

greeter = Greeter(name)
greeter.greet()

Commit the Conflict Resolution

Execute:

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

Output:

$ git add lib/hello.py
$ git commit -m "Merged main fixed conflict."
[greet e118dc5] 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.