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

后端 未结 26 1898
清酒与你
清酒与你 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:43

    In the spirit of answering questions that are somewhat different than asked. Try this solution:

    $ meld my_project_using_git
    

    Meld understands git and provides navigating around the recent changes.

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

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

    $ git config difftool.bc3.cmd "git-diff-bcomp-wrapper.sh \$LOCAL \$REMOTE"
    $ cat git-diff-bcomp-wrapper.sh
    #!/bin/sh
    "c:/Program Files (x86)/Beyond Compare 3/BComp.exe" `cygpath -w $1` `cygpath -w $2`
    
    0 讨论(0)
  • 2020-11-22 03:45

    A short summary of the above great answers:

    git difftool --tool-help
    git config --global diff.tool <chosen tool>
    git config --global --add difftool.prompt false
    

    Then use it by typing (optionally specifying file name as well):

    git difftool
    
    0 讨论(0)
  • 2020-11-22 03:45

    Introduction

    For reference I'd like to include my variation on VonC's answer. Keep in mind that I am using the MSys version of Git (1.6.0.2 at this time) with modified PATH, and running Git itself from Powershell (or cmd.exe), not the Bash shell.

    I introduced a new command, gitdiff. Running this command temporarily redirects git diff to use a visual diff program of your choice (as opposed to VonC's solution that does it permanently). This allows me to have both the default Git diff functionality (git diff) as well as visual diff functionality (gitdiff). Both commands take the same parameters, so for example to visually diff changes in a particular file you can type

    gitdiff path/file.txt
    

    Setup

    Note that $GitInstall is used as a placeholder for the directory where Git is installed.

    1. Create a new file, $GitInstall\cmd\gitdiff.cmd

      @echo off
      setlocal
      for /F "delims=" %%I in ("%~dp0..") do @set path=%%~fI\bin;%%~fI\mingw\bin;%PATH%
      if "%HOME%"=="" @set HOME=%USERPROFILE%
      set GIT_EXTERNAL_DIFF=git-diff-visual.cmd
      set GIT_PAGER=cat
      git diff %*
      endlocal
      
    2. Create a new file, $GitInstall\bin\git-diff-visual.cmd (replacing [visual_diff_exe] placeholder with full path to the diff program of your choice)

      @echo off
      rem diff is called by git with 7 parameters:
      rem path old-file old-hex old-mode new-file new-hex new-mode
      echo Diffing "%5"
      "[visual_diff_exe]" "%2" "%5"
      exit 0
      
    3. You're now done. Running gitdiff from within a Git repository should now invoke your visual diff program for every file that was changed.

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

    On Mac OS X,

    git difftool -t diffuse 
    

    does the job for me in the git folder. For installing diffuse, one can use port -

    sudo port install diffuse
    
    0 讨论(0)
  • 2020-11-22 03:52

    Solution for Windows/msys git

    After reading the answers, I discovered a simpler way that involves changing only one file.

    1. Create a batch file to invoke your diff program, with argument 2 and 5. This file must be somewhere in your path. (If you don't know where that is, put it in c:\windows). Call it, for example, "gitdiff.bat". Mine is:

      @echo off
      REM This is gitdiff.bat
      "C:\Program Files\WinMerge\WinMergeU.exe" %2 %5
      
    2. Set the environment variable to point to your batch file. For example:GIT_EXTERNAL_DIFF=gitdiff.bat. Or through powershell by typing git config --global diff.external gitdiff.bat.

      It is important to not use quotes, or specify any path information, otherwise it won't work. That's why gitdiff.bat must be in your path.

    Now when you type "git diff", it will invoke your external diff viewer.

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