Resolving Conflicts

Goals

  • Learn how to handle conflicts during a merge

Merge master to greet

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

Execute:

  1. git checkout greet
  2. git merge master

Output:

  1. $ git checkout greet
  2. Switched to branch 'greet'
  3. $ git merge master
  4. Auto-merging lib/hello.rb
  5. CONFLICT (content): Merge conflict in lib/hello.rb
  6. Automatic merge failed; fix conflicts and then commit the result.

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

lib/hello.rb

  1. <<<<<<< HEAD
  2. require 'greeter'
  3. # Default is World
  4. name = ARGV.first || "World"
  5. greeter = Greeter.new(name)
  6. puts greeter.greet
  7. =======
  8. # Default is World
  9. puts "What's your name"
  10. my_name = gets.strip
  11. puts "Hello, #{my_name}!"
  12. >>>>>>> master

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

Fix the Conflict

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

lib/hello.rb

  1. require 'greeter'
  2. puts "What's your name"
  3. my_name = gets.strip
  4. greeter = Greeter.new(my_name)
  5. puts greeter.greet

Commit the Conflict Resolution

Execute:

  1. git add lib/hello.rb
  2. git commit -m "Merged master fixed conflict."

Output:

  1. $ git add lib/hello.rb
  2. $ git commit -m "Merged master fixed conflict."
  3. [greet 6938117] Merged master 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.