Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

highlight - How to improve git's diff highlighting?

The output of git diff is optimized for code which tends to be one statement per line whereas text can (if authors like me are too lazy to use line breaks) cause diff output which is very hard to read and more of a "Where's Wally?" search than reading diff output

enter image description here

whereas highlighting as done on GitLab's or GitHub's web frontend shows the difference immediately

enter image description here

I'm aware that I'm comparing HTML and plain text (apples and oranges), however it should be possible to improve the git diff output by using different colors or adding marker characters around a change (JUnit uses [] around insertions which isn't great to read, but an example for what I mean) and it would be the first time that there's something I expect to be somewhere available in git that actually was not.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The word-diff suggested in the other answer isn't exactly what gitlab/github do. To get same effect, you can use diff-highlight script that is distributed with git.

  1. First find path to diff-highlight script. It varies between systems, and is not usually in $PATH. You can find it with your package manager, for example:

    1. Fedora: rpm -ql git | grep diff-highlight
    2. Debian/Ubuntu/Mint: dpkg -L git | grep diff-highlight
    3. Archlinux: pacman -Ql git | grep diff-highlight
  2. Edit ~/.gitconfig, and add to the [pager] section the following (substitute the path):

    [pager]
        # diff-highlight is script provided by git that shows word-by-word diff
        log  = perl /usr/share/git/diff-highlight/diff-highlight | less
        show = perl /usr/share/git/diff-highlight/diff-highlight | less
        diff = perl /usr/share/git/diff-highlight/diff-highlight | less
    

    I'm using perl here instead of calling the script directly because some distros, it seems, do not set executable bit on the script. IMO this is a package bug which should be reported. Anyway, this answer should work disregarding that.

Now log, diff, show commands should show difference word-by-word. Screenshot:

git log -1 -p


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...