Thursday, January 12, 2017

How to configure a git merge tool

Sometimes while using  git, we are brought to solve the conflicts that arise during the merge of the branches.

In some cases, the merge is easy if one or two files are in conflict state. So we can use the vi or other editor to resolve this conflict.

But in other cases, this work becomes tedious.

In this situation, a graphical tool becomes useful.

After some research on google, I found some interesting graphical tools such:

- Meld
sourcegear
- kdiff3 
- vimdiff
- p4merge
- gittortoise


In this post, I will explain how to configure and use kdiff3 to resolve merge conflicts.
It's open source, simple to configure and intuitive...

First, download and install kdiff3 from this link: http://kdiff3.sourceforge.net/

Configuration:

I assume that git is already installed .
And the path of kdiff3 is :"C:/Program Files/KDiff3/kdiff3.exe".

The configuration of kdiff3 is easy, you just need to type the following commands:

1:  $ git config --global --add merge.tool kdiff3  
2:  $ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"  
3:  $ git config --global --add mergetool.kdiff3.trustExitCode false  


Use:

To illustrate the use of the tool, I will cause a conflict during the merge of two branches.

So I assume that we have two branches:
- master
branch-ID-JIRA

I create a file in master branch: file_master.txt

Then, I add some lines in the file and I commit.

For other side, I modified the file in branch-ID-JIRA (same lines)

So during the merge I get a conflict:


To run kdiff3, we use the following command:

 $ git mergetool  

Then the following window will be displayed:


As we can see, we have 4 conflicts:

- Some lines are auto-merged by git( 3 conflicts).
- One conflict should merged.

For this last, we can select which line should be used as following (A, B or C) as following through switch mechanism:


The result of the merge is the following:



Great, no more conflict.

Then, we save and commit to finish the merge.



Hope this short post about git use will be helpful.

Enjoy !




No comments:

Post a Comment

How to configure a git merge tool

Sometimes while using  git, we are brought to solve the conflicts that arise during the merge of the branches. In some cases, the merge i...