Git on Windows: How do you set up a mergetool?
Asked Answered
S

19

380

I've tried msysGit and Git on Cygwin. Both work just fine in and of themselves and both run gitk and git-gui perfectly.

Now how the heck do I configure a mergetool? (Vimdiff works on Cygwin, but preferably I would like something a little more user-friendly for some of our Windows-loving coworkers.)

Sardonic answered 8/1, 2009 at 21:24 Comment(3)
See also https://mcmap.net/q/16432/-p4merge-error-git/867165#867165Dude
Git Extensions has a git UI that has a pretty good, and windows user friendly, merging tool.Lewan
Looks like you need TortoiseGit?Paintbrush
D
340

To follow-up on Charles Bailey's answer, here's my git setup that's using p4merge (free cross-platform 3way merge tool); tested on msys Git (Windows) install:

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'

or, from a windows cmd.exe shell, the second line becomes :

git config --global mergetool.p4merge.cmd "p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""

The changes (relative to Charles Bailey):

  • added to global git config, i.e. valid for all git projects not just the current one
  • the custom tool config value resides in "mergetool.[tool].cmd", not "merge.[tool].cmd" (silly me, spent an hour troubleshooting why git kept complaining about non-existing tool)
  • added double quotes for all file names so that files with spaces can still be found by the merge tool (I tested this in msys Git from Powershell)
  • note that by default Perforce will add its installation dir to PATH, thus no need to specify full path to p4merge in the command

Download: http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools


EDIT (Feb 2014)

As pointed out by @Gregory Pakosz, latest msys git now "natively" supports p4merge (tested on 1.8.5.2.msysgit.0).

You can display list of supported tools by running:

git mergetool --tool-help

You should see p4merge in either available or valid list. If not, please update your git.

If p4merge was listed as available, it is in your PATH and you only have to set merge.tool:

git config --global merge.tool p4merge

If it was listed as valid, you have to define mergetool.p4merge.path in addition to merge.tool:

git config --global mergetool.p4merge.path c:/Users/my-login/AppData/Local/Perforce/p4merge.exe
  • The above is an example path when p4merge was installed for the current user, not system-wide (does not need admin rights or UAC elevation)
  • Although ~ should expand to current user's home directory (so in theory the path should be ~/AppData/Local/Perforce/p4merge.exe), this did not work for me
  • Even better would have been to take advantage of an environment variable (e.g. $LOCALAPPDATA/Perforce/p4merge.exe), git does not seem to be expanding environment variables for paths (if you know how to get this working, please let me know or update this answer)
Downhill answered 12/1, 2009 at 16:41 Comment(20)
Hmm - trying this out - the first config works fine, the next just displays the help text for git config. Not sure why.Kudu
Got it - those single quotes look suspect to me. Just double this up and they work.Kudu
setting mergetool.p4merge.cmd will not work anymore since Git has started trying to support p4merge, see libexec/git-core/git-mergetool--lib . instead it directly uses mergetool.p4merge.pathCuttie
Had to make the second command git config --global mergetool.p4merge.cmd "p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"" on my systemRibbonfish
Had to put the full path and escape double quotes: cmd = \""c:/Program Files/TortoiseSVN/bin/TortoiseMerge.exe"\" -base:"$BASE" -theirs:"$REMOTE" -mine:"$LOCAL" -merged:"$MERGED"Megasporangium
I added p4merge.exe to my PATH, but still had to specify the fully qualified directory location. Also, The escaping of double quotes (or having any at all) wasn't working for me. This answer worked just fine for me.Aramaic
This did it for me: git config --global mergetool.p4merge.cmd '"C:\\Program Files\\Perforce\\p4merge" $BASE $LOCAL $REMOTE $MERGED' on Windows 7 machineMatz
I got the error: p4merge.exe: command not found. git somehow did not read the global path correctly. Solution: Replace p4merge.exe with c:/progra~1/perforce/p4merge.exe. Now it works like a charm.Selectman
Damn, only @DanielPatz's solution worked in this entire thread.Bahena
@subkamran I added a new section for the current msys git; can you confirm that it works for you? (you should delete mergetool.p4merge.cmd and define mergetool.p4merge.path instead)Downhill
Note that the Git from Windows doesn't really work, but the Git from Cygwin (e.g. installed via apt-cyg) does.Ichthyornis
Just in case, when you are doing this on Windows, add quotes to the p4merge path, something like this: git config --global mergetool.p4merge.path "c:/Users/my user/AppData/Local/Perforce/p4merge.exe" My user name has one space and it caused some trouble configuring the tool.Grubb
I posted an helper scripts that irons out few defects when integrating Git and P4Merge: pempek.net/articles/2014/04/18/git-p4mergeCuttie
Since P4Merge is not a Cygwin application, for Cygwin environments you'll need to use cygpath to avoid invalid file errors. For some more detail, see my answer posted hereOogonium
For Git Extensions on win 8 full story was: 1. as @DanielPatz suggested: git config --global mergetool.p4merge.cmd '"C:\\Program Files\\Perforce\\p4merge" $BASE $LOCAL $REMOTE $MERGED' AND 2. Go to Tools -> Settings -> Git Config. Press Suggest button near Mergetool command. Press ApplySqualor
So for Git Extensions on Windows 8 gui only path should be: 1. Install P4Merge 2. In Git Extensions go to Tools -> Settings -> Git Config 3. Choose p4merge in Mergetool dropdown. Press Suggest button near Mergetool command. Press ApplySqualor
Make sure you're using correct quotation, as I needed to adjust for this: https://mcmap.net/q/16432/-p4merge-error-gitDripdry
Bounced around a lot on SO looking for the answer to my problem on this one. As of April, 2016, the Perforce installer automatically adds C:\Program Files\Perforce\DVCS\ to your path. I had to drop the DVCS\ from that entry to catch p4merge.exe in my path. THEN I had to snoop around SO to find a valid config for my Windows 7 MINGW64 setup: git config --global merge.tool p4merge and git config --global mergetool.p4merge.cmd 'p4merge $BASE $LOCAL $REMOTE $MERGED' did the trick and now I am up and r unning!Connivance
p4merge seems introduces encoding issue. anyone found a fix? in settings i changed it to utf-8 but nothing changes.Talich
I don't really understand why, but git mergetool --tool-help does not show p4merge as available, even after setting the .path, but it still works.Cutcherry
P
90

setting mergetool.p4merge.cmd will not work anymore since Git has started trying to support p4merge, see libexec/git-core/git-mergetool--lib.so we just need to specify the mergetool path for git,for example the p4merge:

git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'
git config --global merge.tool p4merge

Then it will work.

Petticoat answered 24/3, 2012 at 11:53 Comment(3)
Works with Beyond Compare 3 as well. Just use the path to BComp.exe instead. Thanks!Ambrosane
I realize this is an old thread, but I can't get that path to work. I'm a bit confused about how the path is supposed to be escaped given the interplay of cmd/msys... EDIT Switching to double quotes fixed it!Edelsten
Thank you Gitninja ! Note that you can type this into Git Bash, but if you use command Prompt you must change the single quote to the double quoteKulda
S
63

I'm using Portable Git on WinXP (works a treat!), and needed to resolve a conflict that came up in branching. Of all the gui's I checked, KDiff3 proved to be the most transparent to use.

But I found the instructions I needed to get it working in Windows in this blog post, instructions which differ slightly from the other approaches listed here. It basically amounted to adding these lines to my .gitconfig file:

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    path = C:/YourPathToBinaryHere/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

Working nicely now!

Stationmaster answered 3/4, 2011 at 12:28 Comment(3)
on my Win7 PC, I had to use path = C:\\Program Files (x86)\\KDiff3\\kdiff3.exe (notice the double back slashes)Morty
I got an error like this "The diff tool meld is not available as 'D:\software\melddiff\Meld.exe'"Monarchal
Do you know if there is a difference with the order where each section is added. I mean, if [merge] would be after [mergetool]?Nitrate
C
20

Under Cygwin, the only thing that worked for me is the following:

git config --global merge.tool myp4merge
git config --global mergetool.myp4merge.cmd 'p4merge.exe "$(cygpath -wla $BASE)" "$(cygpath -wla $LOCAL)" "$(cygpath -wla $REMOTE)" "$(cygpath -wla $MERGED)"'
git config --global diff.tool myp4diff
git config --global difftool.myp4diff.cmd 'p4merge.exe "$(cygpath -wla $LOCAL)" "$(cygpath -wla $REMOTE)"'

Also, I like to turn off the prompt message for difftool:

git config --global difftool.prompt false
Caucasia answered 10/11, 2010 at 23:38 Comment(0)
S
19

git mergetool is fully configurable so you can pretty much chose your favourite tool.

The full documentation is here: http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html

In brief, you can set a default mergetool by setting the user config variable merge.tool.

If the merge tool is one of the ones supported natively by it you just have to set mergetool.<tool>.path to the full path to the tool (replace <tool> by what you have configured merge.tool to be.

Otherwise, you can set mergetool.<tool>.cmd to a bit of shell to be eval'ed at runtime with the shell variables $BASE, $LOCAL, $REMOTE, $MERGED set to the appropriate files. You have to be a bit careful with the escaping whether you directly edit a config file or set the variable with the git config command.

Something like this should give the flavour of what you can do ('mymerge' is a fictional tool).

git config merge.tool mymerge
git config merge.mymerge.cmd 'mymerge.exe --base "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"'

Once you've setup your favourite merge tool, it's simply a matter of running git mergetool whenever you have conflicts to resolve.

The p4merge tool from Perforce is a pretty good standalone merge tool.

Steinman answered 8/1, 2009 at 21:37 Comment(0)
A
11

For beyond compare on Windows 7

git config --global merge.tool bc3
git config --global mergetool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\BCompare.exe"
Arrears answered 25/3, 2014 at 17:56 Comment(1)
I used these commands but I'm using WinMerge instead of Beyond Compare.Illconsidered
B
6

It seems that newer git versions support p4merge directly, so

git config --global merge.tool p4merge

should be all you need, if p4merge.exe is on your path. No need to set up cmd or path.

Brae answered 11/6, 2013 at 6:2 Comment(1)
This is key. After installing p4merge and setting the mergetool.p4merge.path to the exact location, it still did not work. Adding the installation folder to the path did work.Cupulate
N
6

I found two ways to configure "SourceGear DiffMerge" as difftool and mergetool in github Windows.

The following commands in a Command Prompt window will update your .gitconfig to configure GIT use DiffMerge:

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd 'C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe  \"$LOCAL\" \"$REMOTE\"'

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd  'C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe  -merge  -result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\"'

[OR]

Add the following lines to your .gitconfig. This file should be in your home directory in C:\Users\UserName:

[diff]
    tool = diffmerge
[difftool "diffmerge"]
    cmd = C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe \"$LOCAL\" \"$REMOTE\"

[merge]
    tool = diffmerge
[mergetool "diffmerge"]
    trustExitCode = true
    cmd = C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe -merge -result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\"
Nabala answered 17/2, 2017 at 10:52 Comment(0)
B
5

As already answered here (and here and here), mergetool is the command to configure this. For a nice graphical frontend I recommend kdiff3 (GPL).

Biddie answered 8/1, 2009 at 23:38 Comment(0)
E
4

I had to drop the extra quoting using msysGit on windows 7, not sure why.

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge $BASE $LOCAL $REMOTE $MERGED'
Espresso answered 2/8, 2009 at 2:4 Comment(0)
Z
3

If you're doing this through cygwin, you may need to use cygpath:

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge `cygpath -w $BASE` `cygpath -w $LOCAL` `cygpath -w $REMOTE` `cygpath -w $MERGED`'
Zippora answered 30/3, 2010 at 17:39 Comment(0)
Q
3

Bah, this finally worked for me (Windows 7 + Cygwin + TortoiseMerge):

In .git/config:

cmd = TortoiseMerge.exe /base:$(cygpath -d \"$BASE\") /theirs:$(cygpath -d \"$REMOTE\") /mine:$(cygpath -d \"$LOCAL\") /merged:$(cygpath -d \"$MERGED\")

Thanks to previous posters for the tip to use cygpath!

Quenby answered 13/1, 2011 at 15:8 Comment(0)
A
3

i use an app called WinMerge ( http://winmerge.org/ ) info from their manual ( http://manual.winmerge.org/CommandLine.html )

this is the bash script i use from the mergetool directive via .gitconfig

#!/bin/sh
# using winmerge with git
# replaces unix style null files with a newly created empty windows temp file

file1=$1
if [ "$file1" == '/dev/null' ] || [ "$file1" == '\\.\nul' ] || [ ! -e "$file1" ]
    then 
       file1="/tmp/gitnull"
       `echo "">$file1`
fi
file2=$2
if [ "$file2" == '/dev/null' ] || [ "$file2" == '\\.\nul' ] || [ ! -e "$file2" ]
    then 
       file2="/tmp/gitnull"
       `echo "">$file2`
fi
echo diff : $1 -- $2
"C:\Program files (x86)\WinMerge\WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$file1" "$file2"

basically the bash accounts for when the result of the diff in an empty file and creates a new temp file in the correct location.

Able answered 20/12, 2012 at 22:40 Comment(0)
T
1

You may want to add these options too:

git config --global merge.tool p4mergetool
git config --global mergetool.p4merge.cmd 'p4merge $BASE $LOCAL $REMOTE $MERGED'
git config --global mergetool.p4mergetool.trustExitCode false
git config --global mergetool.keepBackup false

Also, I don't know why but the quoting and slash from Milan Gardian's answer screwed things up for me.

Tibetan answered 4/5, 2014 at 21:26 Comment(0)
P
1

If anyone wants to use gvim as their diff tool on TortoiseGit, then this is what you need to enter into the text input for the path to the external diff tool:

path\to\gvim.exe -f -d -c "wincmd R" -c "wincmd R" -c "wincmd h" -c "wincmd J"
Pita answered 24/6, 2014 at 12:57 Comment(0)
G
1

For IntelliJ IDEA (Community Edition) 3-way git mergetool configuration in Windows environment (~/.gitconfig)

Cygwin

[mergetool "ideamerge"]
     cmd = C:/Program\\ Files\\ \\(x86\\)/JetBrains/IntelliJ\\ IDEA\\ Community\\ Edition\\ 14.1.3/bin/idea.exe merge `cygpath -wa $LOCAL` `cygpath -wa $REMOTE` `cygpath -wa $BASE` `cygpath -wa $MERGED`
[merge]
     tool = ideamerge

Msys

[mergetool "ideamerge"]
cmd = "/c/Program\\ Files\\ \\(x86\\)/JetBrains/IntelliJ\\ IDEA\\ Community\\ Edition\\ 14.1.3/bin/idea.exe" merge `~/winpath.sh $LOCAL` `~/winpath.sh $REMOTE` `~/winpath.sh $BASE` `~/winpath.sh $MERGED`
[merge]
 tool = ideamerge

The ~/winpath.sh is to convert paths to Windows on msys and is taken from msys path conversion question on stackoverflow

#! /bin/sh                                                               

function wpath {                                                         
    if [ -z "$1" ]; then                                                 
        echo "$@"                                                        
    else                                                                 
        if [ -f "$1" ]; then                                             
            local dir=$(dirname "$1")                                    
            local fn=$(basename "$1")                                    
            echo "$(cd "$dir"; echo "$(pwd -W)/$fn")" | sed 's|/|\\|g';  
        else                                                             
            if [ -d "$1" ]; then                                         
                echo "$(cd "$1"; pwd -W)" | sed 's|/|\\|g';              
            else                                                         
                echo "$1" | sed 's|^/\(.\)/|\1:\\|g; s|/|\\|g';          
            fi                                                           
        fi                                                               
    fi                                                                   
}                                                                        

wpath "$@" 
Gigantism answered 20/10, 2015 at 1:32 Comment(0)
B
1

For kdiff3 you can use:

git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

git config --global diff.guitool kdiff3
git config --global difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global difftool.kdiff3.trustExitCode false
Braxy answered 3/7, 2020 at 14:48 Comment(0)
C
0

To setup p4merge, installed using chocolatey on windows for both merge and diff, take a look here: https://gist.github.com/CamW/88e95ea8d9f0786d746a

Chemarin answered 20/1, 2015 at 6:15 Comment(0)
S
0

If you're having problems opening p4merge from SourceTree look for you local configuration file named config under MyRepo.git and delete any merge configuration. In my case it was trying to open Meld which I just uninstalled

Simpleminded answered 17/8, 2016 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.