Git is a great cross platform version control system.  However, for windows users it’s got a really steep learning curve, especially when resolving merge conflicts. Working with some people that are just starting to use git, I see a lot of files getting checked back in with the merge markers like “<<<<<<< HEAD” still in the files.

In most compiled languages, leaving that stuff in the source, kind of breaks things. So there should be a way to let my more graphically oriented peers deal with the merges and inevitable conflicts more gracefully. P4merge seemed like it was the most highly thought of  graphical merge tools among the team, so setting that up for merges and diffs would probably be easiest.

Merges

Using a tool to resolve the conflicts is done with the “git mergetool” command. If you’re using the git-gui, right click on the contents of the merged file, and select use merge tool.

gitgui_mergetool

Setting p4merge up as the mergetool for git is really simple. Like most things with git there is more than one way to do it, you can either call “git config” to set the settings, or you can edit either the local or repository specific config files directly.

This is the call to set up p4merge as the merge tool:

 git config --global merge.tool p4merge

That’s it. You’re done. Now when you call “git mergetool” you get the conflicts displayed in p4merge.  To me, there are a couple of problems with this. First is that you get the *.orig files cluttering up the directory. Second, is that you get something like this prompt:

Normal merge conflict for 'one.txt':
 {local}: modified file
 {remote}: modified file
Hit return to start merge resolution tool (p4merge):

*.orig files

To have it stop creating, or more accurately delete, the .orig files, you need to set the keepBackup  option of the merge tool, like this:

git config --global mergetool.keepBackup false

Annoying Prompt

The prompt for which merge tool to use, is only really useful if you have multiple merge tools configured.  Since there is only p4merge, we can dispense with the prompt by setting this:

git config --global mergetool.prompt false

That way git uses the default merge tool automatically without prompting you.

Config File

If you want to edit the config file instead of using the commands to set this up, the changes look like this:

[merge]
   tool = p4merge
[mergetool]
   keepBackup = false
   prompt = false

Those five lines of config file are the equivalent of

git config --global merge.tool p4merge
git config --global mergetool.keepBackup false
git config --global mergetool.prompt false

The next big hurdle will be launching the merge tool automatically.

Diffs

I tend to be pretty command line oriented, but there are sometimes when a visual tool comes in handy. One of those times is when doing diffs. Using p4merge as the diff tool is a little more difficult.  I got the original idea for the diff script from this blog entry: Using p4merge as a git mergetool but I had to tweak it somewhat.

Basically, you need to create a shell script to call p4merge because git supplies the arguments in an order that p4merge doesn’t expect. The easiest way to do this is to edit the .gitconfig file directly, adding this section:

[diff]
 external = "~/bin/differ.bat"

The batch file can be anywhere, or anything for that matter. The important part to remember is that git passes more arguments to the diff executable than p4merge expects, so if you tried to call it directly, you will get an error.

The batch file is really simple, just a single line:

p4merge $2 $1

I use the git bash shell under windows, so the batch file uses the bash style argument names.  This gives me the original version of the file on the left and the changed version of the file on the right when I type “git diff” from the shell window.

 

 

Advertisements