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:
git checkout greet
git merge master
Output:
$ git checkout greet
Switched to branch 'greet'
$ git merge master
Auto-merging lib/hello.rb
CONFLICT (content): Merge conflict in lib/hello.rb
Automatic merge failed; fix conflicts and then commit the result.
If you open lib/hello.rb, you will see:
lib/hello.rb
<<<<<<< HEAD
require 'greeter'
# Default is World
name = ARGV.first || "World"
greeter = Greeter.new(name)
puts greeter.greet
=======
# Default is World
puts "What's your name"
my_name = gets.strip
puts "Hello, #{my_name}!"
>>>>>>> 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
require 'greeter'
puts "What's your name"
my_name = gets.strip
greeter = Greeter.new(my_name)
puts greeter.greet
Commit the Conflict Resolution
Execute:
git add lib/hello.rb
git commit -m "Merged master fixed conflict."
Output:
$ git add lib/hello.rb
$ git commit -m "Merged master fixed conflict."
[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.
当前内容版权归 gitimmersion 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 gitimmersion .