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