Home > mac, source control > GIT Merge and Diff with p4merge on Mac

GIT Merge and Diff with p4merge on Mac

Git is my favorite SCV system up to now and most probably will be in the future. Besides of many great features is has pretty good automatic merge tool, that is able to solve conflicts, but not all of them. I found p4merge tool much better in case of automatic merging and manual solving the most hard conflicts. Is is very simple to mix together those two things to have the best from both of them and now I will show you how to do that.

First of all, you have to install p4merge tool. Go to p4merge tool download page, choose “Browse by platform” tab, pick your platform (in my case it was Macintosh) and download P4V: Visual Client. After downloading, open dmg file and copy p4merge tool to Applications folder in your Dock.

Second step is to create two simple bash scripts: p4merge and p4diff,  that will be executed when git needs merge or diff tool. Create these scripts in directory that is in your’s system PATH (i.e. /usr/local/bin/).

p4merge

#!/bin/sh
/Applications/p4merge.app/Contents/MacOS/p4merge $*

p4diff

#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/p4merge "$2" "$PWD/$5"

Don’t forget to add executable mode/flag to these scripts.

After p4merge and scripts are ready it’s time to configure global git settings. Edit ~/.gitconfig file and put following configuration there for merging:

[merge]
    tool = p4merge
    keepBackup = false
[mergetool "p4merge"]
    cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
    keepTemporaries = false
    trustExitCode = false
    keepBackup = false
[diff]
    external = p4diff

If you don’t want to use p4merge as diff tool, just comment (using # character) [diff] section. Sometimes it’s faster for me to use command line diff instead of visual one.

Now, when you type “git diff” or “git mergetool” in your’s project directory, p4merge tool will be started.

Advertisements
Categories: mac, source control Tags: , , , , ,
  1. February 16, 2012 at 13:47

    /s/Thirst/First
    (“Thirst of all, you have to install p4merge tool.”)

    • February 16, 2012 at 13:50

      I fixed this mistake, thanks!

  2. October 17, 2012 at 22:32

    Thank you. Your post was very helpful!

  3. December 14, 2012 at 20:32

    I believe your /usr/local/bin/p4diff file contents should be (notice the p4merge path):

    #!/bin/sh
    [ $# -eq 7 ] && /usr/local/bin/p4merge “$2” “$PWD/$5”

    • December 27, 2012 at 09:01

      Yes, you are right. Fixed now. Thanks!

  1. February 1, 2013 at 16:37

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

%d bloggers like this: