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

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

提交回复
热议问题