Semantic merge as the default merge tool with git on Windows

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 toolsemanticmerge.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!

Further reading

Advertisements

5 thoughts on “Semantic merge as the default merge tool with git on Windows

  1. Bart

    SemanticMerge is a godsend for merging. Can you share the configuration for setting it up as the Git difftool?

    Thanks!

    Reply
    1. Anthony

      @Bart – here is something that I have whipped up (if you haven’t discovered this for yourself already):
      Note: I am using cygwin, so the following commands are using unix style paths.
      [diff]
      tool = SemanticMerge
      external = /cygdrive/c/Users//AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d=`cygpath -w \”$5\”` -s=`cygpath -w \”$2\”` -edt=\”mergetool.exe -s=\”\”@sourcefile\”\” -sn=\”\”@sourcesymbolic\”\” -d=\”\”@destinationfile\”\” -dn=\”\”@destinationsymbolic\”\” -t=\”\”@filetype\”\” -i=\”\”@comparationmethod\”\” -e=\”\”@fileencoding\”\”\”

      [difftool “SemanticMerge”]
      cmd = /cygdrive/c/Users//AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d=`cygpath -w \”$LOCAL\”` -s=`cygpath -w \”$REMOTE\”` -edt=\”mergetool.exe -s=\”\”@sourcefile\”\” -sn=\”\”@sourcesymbolic\”\” -d=\”\”@destinationfile\”\” -dn=\”\”@destinationsymbolic\”\” -t=\”\”@filetype\”\” -i=\”\”@comparationmethod\”\” -e=\”\”@fileencoding\”\”\”

      Reply
  2. Pingback: Git mergetool on Windows keeps being given filepath from Git install rather than repository root | BlogoSfera

  3. Pingback: Telling git to follow moved content (not simply moved files) | Ngoding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s