How do I view 'git diff' output with my preferred diff tool/ viewer?

后端 未结 26 1863
清酒与你
清酒与你 2020-11-22 03:20

When I type git diff, I want to view the output with my visual diff tool of choice (SourceGear \"diffmerge\" on Windows). How do I configure git to do this?

相关标签:
26条回答
  • 2020-11-22 03:58

    I have one addition to this. I like to regularly use a diff app that isn't supported as one of the default tools (e.g. kaleidoscope), via

    git difftool -t
    

    I also like to have the default diff just be the regular command line, so setting the GIT_EXTERNAL_DIFF variable isn't an option.

    You can use an arbitrary diff app as a one-off with this command:

    git difftool --extcmd=/usr/bin/ksdiff
    

    It just passes the 2 files to the command you specify, so you probably don't need a wrapper either.

    0 讨论(0)
  • 2020-11-22 03:59

    After looking at some other external diff tools, I found that the diff view in IntelliJ IDEA (and Android Studio) is the best one for me.

    Step 1 - setup IntelliJ IDEA to be run from the command line

    If you want to use IntelliJ IDEA as your diff tool you should first setup IntelliJ IDEA to be run from the command line following the instructions here:

    On macOS or UNIX:

    1. Make sure IntelliJ IDEA is running.
    2. On the main menu, choose Tools | Create Command-line Launcher. The dialog box Create Launcher Script opens, with the suggested path and name of the launcher script. You can accept default, or specify your own path. Make notice of it, as you'll need it later. Outside of IntelliJ IDEA, add the path and name of the launcher script to your path.

    On Windows:

    1. Specify the location of the IntelliJ IDEA executable in the Path system environment variable. In this case, you will be able to invoke the IntelliJ IDEA executable and other IntelliJ IDEA commands from any directory.

    Step 2 - configure git to use IntelliJ IDEA as the difftool

    Following the instructions on this blog post:

    Bash

    export INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
    PATH=$IDEA_HOME $PATH
    

    Fish

    set INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
    set PATH $INTELLIJ_HOME $PATH
    

    Now add the following to your git config:

    [merge]
       tool = intellij
    [mergetool "intellij"]
       cmd = idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
       trustExitCode = true
    [diff]
       tool = intellij
    [difftool "intellij"]
       cmd = idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")
    

    You can try it out with git difftool or git difftool HEAD~1

    0 讨论(0)
  • 2020-11-22 04:00

    Since git version 1.6.3 there is "git difftool" which you can configure to use your favorite graphical diff tool. Currently supported out-of-the-box are kdiff3, kompare, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, diffuse and opendiff; if the tool you want to use isn't on this list, you can always use 'difftool.<tool>.cmd' configuration option.

    "git difftool" accepts the same options as "git diff".

    0 讨论(0)
  • 2020-11-22 04:02

    Since Git1.6.3, you can use the git difftool script: see my answer below.


    May be this article will help you. Here are the best parts:

    There are two different ways to specify an external diff tool.

    The first is the method you used, by setting the GIT_EXTERNAL_DIFF variable. However, the variable is supposed to point to the full path of the executable. Moreover, the executable specified by GIT_EXTERNAL_DIFF will be called with a fixed set of 7 arguments:

    path old-file old-hex old-mode new-file new-hex new-mode
    

    As most diff tools will require a different order (and only some) of the arguments, you will most likely have to specify a wrapper script instead, which in turn calls the real diff tool.

    The second method, which I prefer, is to configure the external diff tool via "git config". Here is what I did:

    1) Create a wrapper script "git-diff-wrapper.sh" which contains something like

    -->8-(snip)--
    #!/bin/sh
    
    # diff is called by git with 7 parameters:
    # path old-file old-hex old-mode new-file new-hex new-mode
    
    "<path_to_diff_executable>" "$2" "$5" | cat
    --8<-(snap)--
    

    As you can see, only the second ("old-file") and fifth ("new-file") arguments will be passed to the diff tool.

    2) Type

    $ git config --global diff.external <path_to_wrapper_script>
    

    at the command prompt, replacing with the path to "git-diff-wrapper.sh", so your ~/.gitconfig contains

    -->8-(snip)--
    [diff]
        external = <path_to_wrapper_script>
    --8<-(snap)--
    

    Be sure to use the correct syntax to specify the paths to the wrapper script and diff tool, i.e. use forward slashed instead of backslashes. In my case, I have

    [diff]
        external = \"c:/Documents and Settings/sschuber/git-diff-wrapper.sh\"
    

    in .gitconfig and

    "d:/Program Files/Beyond Compare 3/BCompare.exe" "$2" "$5" | cat
    

    in the wrapper script. Mind the trailing "cat"!

    (I suppose the '| cat' is needed only for some programs which may not return a proper or consistent return status. You might want to try without the trailing cat if your diff tool has explicit return status)

    (Diomidis Spinellis adds in the comments:

    The cat command is required, because diff(1), by default exits with an error code if the files differ.
    Git expects the external diff program to exit with an error code only if an actual error occurred, e.g. if it run out of memory.
    By piping the output of git to cat the non-zero error code is masked.
    More efficiently, the program could just run exit with and argument of 0.)


    That (the article quoted above) is the theory for external tool defined through config file (not through environment variable).
    In practice (still for config file definition of external tool), you can refer to:

    • How do I setup DiffMerge with msysgit / gitk? which illustrates the concrete settings of DiffMerge and WinMerge for MsysGit and gitk
    • How can I set up an editor to work with Git on Windows? for the definition of Notepad++ as an external editor.
    0 讨论(0)
  • 2020-11-22 04:02

    Here's a batch file that works for Windows - assumes DiffMerge installed in default location, handles x64, handles forward to backslash replacement as necessary and has ability to install itself. Should be easy to replace DiffMerge with your favourite diff program.

    To install:

    gitvdiff --install 
    

    gitvdiff.bat:

    @echo off
    
    REM ---- Install? ----
    REM To install, run gitvdiff --install
    
    if %1==--install goto install
    
    
    
    REM ---- Find DiffMerge ----
    
    if DEFINED ProgramFiles^(x86^) (
        Set DIFF="%ProgramFiles(x86)%\SourceGear\DiffMerge\DiffMerge.exe"
    ) else (
        Set DIFF="%ProgramFiles%\SourceGear\DiffMerge\DiffMerge.exe"
    )
    
    
    
    REM ---- Switch forward slashes to back slashes ----
    
    set oldW=%2
    set oldW=%oldW:/=\%
    set newW=%5
    set newW=%newW:/=\%
    
    
    REM ---- Launch DiffMerge ----
    
    %DIFF% /title1="Old Version" %oldW% /title2="New Version" %newW%
    
    goto :EOF
    
    
    
    REM ---- Install ----
    :install
    set selfL=%~dpnx0
    set selfL=%selfL:\=/%
    @echo on
    git config --global diff.external %selfL%
    @echo off
    
    
    :EOF
    
    0 讨论(0)
  • 2020-11-22 04:02

    If you're on a Mac and have XCode, then you have FileMerge installed. The terminal command is opendiff, so you can just do git difftool -t opendiff

    0 讨论(0)
提交回复
热议问题