Skip to content

Instantly share code, notes, and snippets.

@tony4d
Created August 24, 2012 19:00
Show Gist options
  • Save tony4d/3454372 to your computer and use it in GitHub Desktop.
Save tony4d/3454372 to your computer and use it in GitHub Desktop.
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/p4merge.app/Contents/Resources/launchp4merge \$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/p4merge.app/Contents/Resources/launchp4merge \$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.
@CAMOBAP
Copy link

CAMOBAP commented Mar 16, 2014

Are you sure about order in
"/Applications/p4merge.app/Contents/Resources/launchp4merge $PWD/$BASE $PWD/$REMOTE $PWD/$LOCAL $PWD/$MERGED"

Looks like it should be
"/Applications/p4merge.app/Contents/Resources/launchp4merge $PWD/$BASE $PWD/$LOCAL $PWD/$REMOTE $PWD/$MERGED"

Correct me if I'm wrong

@ahsanra
Copy link

ahsanra commented Mar 30, 2014

I guess you are right camobap

@jerryni
Copy link

jerryni commented Jul 11, 2014

It works. Thanks!

@mangotang
Copy link

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

http://git-scm.com/book/en/Customizing-Git-Git-Configuration

@gpakosz
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 http://pempek.net/articles/2014/04/18/git-p4merge/

Hope that helps

@feigaoxyz
Copy link

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

@tpowell01
Copy link

tford!

@mikemichaelis
Copy link

@juanfrnz
Copy link

juanfrnz commented Jan 3, 2018

Thanks for sharing!

@terevos
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"

@21region
Copy link

This instruction is obsolete and doesn't work.

@jenhsun
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 https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration and https://community.perforce.com/s/article/2848

  1. $ brew cask install p4merge
  2. $ sudo vim /usr/local/bin/extMerge and add below lines:
#!/bin/sh
/Applications/p4merge.app/Contents/MacOS/p4merge $*

from preforce, the /Applications/p4merge.app/Contents/Resources/launchp4merge 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:
#!/bin/sh  
/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

[merge]
	tool = extMerge
	stat = true
[mergetool "extMerge"]
	cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
	keepBackup = false
	keepTemporaries = false
	trustExitCode = false
	prompt = false
[diff]
	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.

@seperman
Copy link

seperman commented Oct 8, 2018

👍

@alex-rogachev
Copy link

@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!

@rayray
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?

EDIT:

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.

#!/bin/sh

/Applications/p4merge.app/Contents/MacOS/p4merge "$1" "$2" "$3" "$4"

@rizowski
Copy link

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/p4merge.app/Contents/MacOS/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"

It used to be

[mergetool "p4merge"]
  cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "\"$PWD/$BASE\"" "\"$PWD/$REMOTE\"" "\"$PWD/$LOCAL\"" "\"$PWD/$MERGED\""

Versions:

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

@dinobu
Copy link

dinobu commented Oct 17, 2018

I guess you are right camobap

why not correct it then?

@chrisnicholls
Copy link

I guess you are right camobap

why not correct it then?

+1

@arefed-zz
Copy link

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

@WesBAn
Copy link

WesBAn commented Apr 25, 2019

Thanks!

@UnreachableCode
Copy link

After I resolve a merge, I get /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git-mergetool--lib: line 128: 8522 Segmentation fault: 11 /Applications/p4merge.app/Contents/Resources/launchp4merge $PWD/$BASE $PWD/$REMOTE $PWD/$LOCAL $PWD/$MERGED. How can I fix this?

@ismusidhu
Copy link

ismusidhu commented Jul 3, 2020

merge config given at https://apple.stackexchange.com/a/346038/267749 worked instead for me.
P4Merge version -> Rev. P4Merge/MACOSX1015X86_64/2020.1/1966006
Git version -> git version 2.24.3 (Apple Git-128)

@louim
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/p4merge.app/Contents/Resources/launchp4merge

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:

[diff]
  tool = p4merge
[merge]
  tool = p4merge
[mergetool "p4merge"]
  path = /Applications/p4merge.app/Contents/Resources/launchp4merge

@sebkraemer
Copy link

Works perfectly for me, @louim, thanks 👍

@umutseven92
Copy link

Thank you @louim, works like a charm 👍

@bogre
Copy link

bogre commented Jun 10, 2022

Does it prompts you for files when you run git diff-tool master..develop for example?
For me, it does and shouldn't be like that.

@pboling
Copy link

pboling commented Aug 21, 2023

There is a window that asks about the paths, but there is also a confirmation dialog that asks if it is OK for an Applescript to be run, and when you approve it to run the script (which you only have to do once), then the path window disappears and the merge opens as expected. I installed via homebrew.

@shivanraptor
Copy link

Still works perfectly in 2023. macOS Ventura

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