Why doesn't `git diff` invoke external diff tool?

前端 未结 3 1890
执笔经年
执笔经年 2021-02-02 08:22

In my repository, if I type

$ git diff some-file

or

$ git difftool some-file

I get the in-terminal diff displ

相关标签:
3条回答
  • 2021-02-02 08:39

    I get the in-terminal diff display. I this should not happen, because I have set up an external diff tool

    Yes, it should: diff.external is for "in-terminal diff display".

    (from git config man page)

    diff.external
    

    If this config variable is set, diff generation is not performed using the internal diff machinery, but using the given command.
    Can be overridden with the GIT_EXTERNAL_DIFF environment variable.
    The command is called with parameters as described under "git Diffs" in git(1). Note: if you want to use an external diff program only on a subset of your files, you might want to use gitattributes(5) instead.

    The question you link explains why meld wouldn't be able to play the role of an "external diff".

    Viewing a diff visually with another tool is done with:

    git difftool --dir-diff shaOfHisCheckIn^!
    git difftool --tool=meld --dir-diff shaOfHisCheckIn^!
    git difftool -t meld -d shaOfHisCheckIn^!
    

    meld can be configured on Windows as a difftool: see "Git Diff and Meld on Windows".


    If you wanted to configure meld for git diff, you could (on Ubuntu) use the diff.external, but with a wrapper script:

    create a file called git-diff.sh, using the following content:

    #!/bin/bash
    meld "$2" "$5" > /dev/null 2>&1
    

    Save this to a location such as /usr/local/bin, giving it executable rights:

    $ sudo mv git-diff.sh /usr/local/bin/
    $ sudo chmod +x /usr/local/bin/git-diff.sh
    

    The final step is to open your $HOME/.gitconfig file and add the following few lines:

    [diff]
            external = /usr/local/bin/git-diff.sh
    

    The next time you type git diff in a Git project with changes, Meld will be launched showing you a split-pane diff viewer.
    Note that you are required to close the open instance of meld before the next diff viewer is opened.

    0 讨论(0)
  • 2021-02-02 08:40

    It seems that git diff (at least, as of git version 1.7.12.4) will not run anything other than the internal, console-only diff on a file which is in the "both modified" state. git mergetool works on such files, though.

    0 讨论(0)
  • 2021-02-02 08:50

    I usually just want to check whether the changes I'm about to check in are correct. So I followed the procedure suggested here (similar to the one noted by VonC). However, running git difftool still didn't open meld. I then created an alias:

    alias git-diff='git difftool $(git rev-parse HEAD)'
    

    Save this in your .bashrc or .zshrc or the corresponding config for your shell. This essentially compares the state of the branch with the previous commit on the branch.

    Do a git-diff to see changes on a file per file basis or git-diff --dir to see all changes in a directory view.

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