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

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

提交回复
热议问题