Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Setup p4merge as a visual diff and merge tool for git

Download and install p4merge

Setup p4merge as a visual mergetool

$ git config --global merge.tool p4mergetool
$ git config --global mergetool.p4mergetool.cmd \
"/Applications/ \$PWD/\$BASE \$PWD/\$REMOTE \$PWD/\$LOCAL \$PWD/\$MERGED"
$ git config --global mergetool.p4mergetool.trustExitCode false
$ git config --global mergetool.keepBackup false

Setup p4merge as a visual diff tool

$ git config --global diff.tool p4mergetool
$ git config --global difftool.p4mergetool.cmd \
"/Applications/ \$LOCAL \$REMOTE"

Using p4merge to resolve conflicts

When you run into a conflict when merging simply run:

$ git mergetool
  • You will be prompted to run "p4mergetool", hit enter and the visual merge editor will launch.
  • Using the merge tool you can resolve the conflict and then save the file.
  • After you exit the merge tool take a look back at your terminal. You will be asked if the merge was successful, choose yes if all is well or no if you need to start over.
  • This prompting is happening because the "trustExitCode" option is turned off. Personally I always like to be asked, but you can have git just trust the exit code from the merge tool.
Copy link

zezba9000 commented Jan 25, 2014

You should add How To for Windows and Linux.

Copy link

CAMOBAP commented Mar 16, 2014

Are you sure about order in

Looks like it should be

Correct me if I'm wrong

Copy link

ahsanra commented Mar 30, 2014

I guess you are right camobap

Copy link

jerryni commented Jul 11, 2014

It works. Thanks!

Copy link

mangotang commented Aug 25, 2014

@zzba9000: The GIT docs explain how to hook GIT up with p4merge/p4diff:

Copy link

gpakosz commented Nov 1, 2014

I made a launcher script that irons things out when using Git + P4Merge, I explain the setup in a blog post

Hope that helps

Copy link

feigaoxyz commented Dec 24, 2014

I think your need quotation marks " surround $XX in case white spaces in filenames.

Copy link

tpowell01 commented Dec 2, 2015


Copy link

mikemichaelis commented Apr 19, 2017

Copy link

juanantoniofj commented Jan 3, 2018

Thanks for sharing!

Copy link

terevos commented Apr 16, 2018

These instructions don't actually work, at least not on Mac OS X 10.12 or 10.13.

Use this URL under the heading: "External Merge and Diff Tools"

Copy link

21region commented Aug 31, 2018

This instruction is obsolete and doesn't work.

Copy link

jenhsun commented Oct 8, 2018

Hey, guys. I would like to share my working p4merge(2018.2-1666551) setup on macOS Mojave from both and

  1. $ brew cask install p4merge
  2. $ sudo vim /usr/local/bin/extMerge and add below lines:
/Applications/ $*

from preforce, the /Applications/ is not working on me. However, above 'none-shim' method can bring up p4merge windows app. So weird.

  1. $ sudo vim /usr/local/bin/extDiff and add below lines:
/usr/local/bin/extMerge "$2" "$5"
  1. $ sudo chmod +x /usr/local/bin/extMerge and
    $ sudo chmod +x /usr/local/bin/extDiff

  2. my ~/.gitconfig

	tool = extMerge
	stat = true
[mergetool "extMerge"]
	cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
	keepBackup = false
	keepTemporaries = false
	trustExitCode = false
	prompt = false
	external = extDiff
	renames = copies
	mnemonicprefix = true
[difftool "extDiff"]
	cmd = extDiff "$LOCAL" "$REMOTE"
	keepBackup = false
	keepTemporaries = false
	trustExitCode = false
	prompt = false

That's it. I hope these works on your machine.

Copy link

seperman commented Oct 8, 2018


Copy link

alex-rogachev commented Oct 10, 2018

@jenhsun Phew, finally it works, thanks a lot sir! For some reason after update to Mojave p4merge started asking me to choose files to merge manually, it just drove me crazy but your config works fine!

Copy link

rayray commented Oct 11, 2018

@jenhsun This gets me closer but that doesn't seem to escape spaces in the filepath, leading to p4merge complaining about too many arguments.

I've tried the following:

cmd = mymerge ${BASE} ${LOCAL} ${REMOTE} ${MERGED}
# and this one fails too
cmd = mymerge "${BASE}" "${LOCAL}" "${REMOTE}" "${MERGED}"

But p4merge fails with the same error on both. How do I make sure spaces are escaped?


I ended up having to explicitly enumerate the arguments in the shell script, the $* does not seem to carry the quotes/spaces over.

This is fine, since I never expect the number of arguments to change.


/Applications/ "$1" "$2" "$3" "$4"

Copy link

rizowski commented Oct 11, 2018

Not sure why it stops working in Mac OSX Mojave. But I had to move from the shim to the app so just doing this worked for me:

[mergetool "p4merge"]
  cmd = /Applications/ "$BASE" "$LOCAL" "$REMOTE" "$MERGED"

It used to be

[mergetool "p4merge"]
  cmd = /Applications/ "\"$PWD/$BASE\"" "\"$PWD/$REMOTE\"" "\"$PWD/$LOCAL\"" "\"$PWD/$MERGED\""


  • git version 2.19.1
  • Mojave 10.14 (18A391)
  • Rev. P4Merge/MACOSX1013X86_64/2018.3/1706936

Copy link

dinobu commented Oct 17, 2018

I guess you are right camobap

why not correct it then?

Copy link

chrisnicholls commented Nov 29, 2018

I guess you are right camobap

why not correct it then?


Copy link

arefed-zz commented Dec 12, 2018

if you want git to skip prompting for confirmation to open every single file do this:
git config --global --add difftool.prompt false

Copy link

WesBAn commented Apr 25, 2019


Copy link

UnreachableCode commented Aug 8, 2019

After I resolve a merge, I get /Applications/ line 128: 8522 Segmentation fault: 11 /Applications/ $PWD/$BASE $PWD/$REMOTE $PWD/$LOCAL $PWD/$MERGED. How can I fix this?

Copy link

ismusidhu commented Jul 3, 2020

merge config given at worked instead for me.
P4Merge version -> Rev. P4Merge/MACOSX1015X86_64/2020.1/1966006
Git version -> git version 2.24.3 (Apple Git-128)

Copy link

louim commented Feb 1, 2021

Most recent version of git already have included configuration for P4Merge. The only thing you need to do on a Mac is:

# setup p4merge as the default difftool
git config --global diff.tool p4merge

# setup p4merge as the default mergetool
git config --global merge.tool p4merge

# Define the path to P4merge launcher.
git config --global mergetool.p4merge.path /Applications/

Note the p4merge instead of p4mergetool. This will setup P4Merge with the correct launch arguments. This was tested with:

  • P4Merge/MACOSX1015X86_64/2020.3/2060285
  • MacOS Catalina 10.15.7 (19H114)
  • git version 2.30.0 (also tested with git version 2.24.1)

At the end, the only required configuration related to P4 you need in your .gitconfig (excluding your personal preferences) should be:

  tool = p4merge
  tool = p4merge
[mergetool "p4merge"]
  path = /Applications/

Copy link

sebkraemer commented Feb 19, 2021

Works perfectly for me, @louim, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment