Instantly share code, notes, and snippets.

Embed
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/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.
@zezba9000

This comment has been minimized.

Copy link

zezba9000 commented Jan 25, 2014

You should add How To for Windows and Linux.

@CAMOBAP795

This comment has been minimized.

Copy link

CAMOBAP795 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

This comment has been minimized.

Copy link

ahsanra commented Mar 30, 2014

I guess you are right camobap

@jerryni

This comment has been minimized.

Copy link

jerryni commented Jul 11, 2014

It works. Thanks!

@mangotang

This comment has been minimized.

Copy link

mangotang commented Aug 25, 2014

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

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

@gpakosz

This comment has been minimized.

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

This comment has been minimized.

Copy link

feigaoxyz commented Dec 24, 2014

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

@tpowell01

This comment has been minimized.

Copy link

tpowell01 commented Dec 2, 2015

tford!

@mikemichaelis

This comment has been minimized.

Copy link

mikemichaelis commented Apr 19, 2017

@juanantoniofj

This comment has been minimized.

Copy link

juanantoniofj commented Jan 3, 2018

Thanks for sharing!

@terevos

This comment has been minimized.

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

This comment has been minimized.

Copy link

21region commented Aug 31, 2018

This instruction is obsolete and doesn't work.

@jenhsun

This comment has been minimized.

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

This comment has been minimized.

Copy link

seperman commented Oct 8, 2018

👍

@alex-rogachev

This comment has been minimized.

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!

@rayray

This comment has been minimized.

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

This comment has been minimized.

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/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

This comment has been minimized.

Copy link

dinobu commented Oct 17, 2018

I guess you are right camobap

why not correct it then?

@chrisnicholls

This comment has been minimized.

Copy link

chrisnicholls commented Nov 29, 2018

I guess you are right camobap

why not correct it then?

+1

@arefed

This comment has been minimized.

Copy link

arefed 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

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