When working on a version control system like git, mercurial or plasticscm, branching and merging are part of our daily work. The merge operation may cause conflicts, and usually we must manually solve them using 3-way merge tools.
By default git won’t provide a tool for this operation, so in this article we will see how to configure an external tool (in our case our Semantic Merge tool: semanticmerge.exe) on git in the Windows platform.
Setting git configuration
One of the most common ways of setting the configuration is by using the git config command, for example:
git config --global core.editor emacs
Will set the default text editor. We have the –global modifier, that saves the configuration at user level, we will talk about the levels in the next section. This operation saves the following data in our configuration file:
[core] editor = emacs
Git stores the configuration in a .config file at three different levels:
- repository level, located inside our local repository at .git\config (git config without extra modifiers)
- user level, usually located at C:\Users\Me\.gitconfig on windows) (–global modifier)
- system level, located in our git instalation folder, for example C:\Program Files\Git (–system modifier)
Setting the custom merge tool
Now it’s easy to set the merge tool inside the configuration file. The first thing we need is to define a name for our tool:
[merge] tool = SemanticMerge
Afterwards, for the selected tool, we must define the path for the executable that will be launched, inside it’s own section:
[mergetool "SemanticMerge"] path = C:/Program Files/PlasticSCM4/semanticmerge/semanticmergetool.exe
Aditionally, we must set a couple more parameters:
[mergetool "SemanticMerge"] keepBackup = false trustExitCode = false
Finally, we must set the parameters that will be passed to the tool, in this case, the source files, destination and base.
[mergetool "SemanticMerge"] ... cmd = \"C:/Program Files/PlasticSCM4/semanticmerge/semanticmergetool.exe\" -b=\"$BASE\" -d=\"$LOCAL\" -s=\"$REMOTE\" -r=\"$MERGED\" -l=csharp -emt=\"mergetool.exe -b=\"\"@basefile\"\" -bn=\"\"@basesymbolic\"\" -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -r=\"\"@output\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\" -edt=\"mergetool.exe -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\"
This is the final result:
[merge] tool = SemanticMerge [mergetool "SemanticMerge"] path = C:/Program Files/PlasticSCM4/semanticmerge/semanticmergetool.exe keepBackup = false trustExitCode = false cmd = \"C:/Program Files/PlasticSCM4/semanticmerge/semanticmergetool.exe\" -b=\"$BASE\" -d=\"$LOCAL\" -s=\"$REMOTE\" -r=\"$MERGED\" -l=csharp -emt=\"mergetool.exe -b=\"\"@basefile\"\" -bn=\"\"@basesymbolic\"\" -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -r=\"\"@output\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\" -edt=\"mergetool.exe -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\"
Now, if we generate a conflict, our semantic merge tool will launch for resolving it, displaying a message like this:
git mergetool Merging: base.cs Normal merge conflict for 'base.cs': {local}: modified file {remote}: modified file Hit return to start merge resolution tool (SemanticMerge):
Happy hacking!