46

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.

1

6 Answers 6

50

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. Now, execute the following two commands, which will add to your ~/.gitconfig the necessary settings:

    $ git config --global core.pager 'perl /usr/share/git/diff-highlight/diff-highlight | less'
    $ git config --global interactive.difffilter 'perl /usr/share/git/diff-highlight/diff-highlight'
    

    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 regardless.

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

git log -1 -p

10
  • 2
    My answer is very close to yours and it was pointed rightfully that I should probably remove it. Would you be ok indicating how to find the path (find -L /usr -name diff-highlight -type f) and make the script executable (sudo chmod +x /usr/share/doc/git/contrib/diff-highlight/diff-highlight) and I will then remove my answer?
    – Zorglub29
    Commented Apr 7, 2020 at 7:04
  • 1
    @Zorglub29 okay, so, I incorporated your suggestions. I was considering find command with a few hacks to get the path which would be the /usr for us, and would work on a wider range of distros. But after I run find /usr, and it didn't return for 10 seconds till I stopped it, I figured using find is a bad idea. It is slow and it pollutes files cache for no reason. I added a few examples of package manager usage instead. Regarding the executable bit not being set, note that after git package update your changes gonna be lost. To work around that I'm calling perl directly in the answer.
    – Hi-Angel
    Commented Apr 7, 2020 at 18:34
  • 1
    The recent edits have really improved this answer. For what it's worth, I had used the locate utility to get the path for the diff-highlight script (it was already executable on recent Fedora but not on Ubuntu 16.04). Commented Apr 7, 2020 at 20:43
  • 4
    On Debian testing (bullseye) it's in doc (?!) and there's no diff-highlight/diff-highlight; closest i could see was diff-highlight/diff-highlight.perl and that returns this error: Undefined subroutine &DiffHighlight::highlight_stdin called at /usr/share/doc/git/contrib/diff-highlight/diff-highlight.perl line 7.
    – mlncn
    Commented Jun 9, 2021 at 11:43
  • 4
    Update to above comment regarding Debian testing: cd /usr/share/doc/git/contrib/diff-highlight then: sudo make And i have my executable diff-highlight.
    – mlncn
    Commented Jun 9, 2021 at 11:54
37

Also worth mentioning is diffr. It's written in Rust and uses Myers longest common subsequence algorithm. Compared to git's diff-highlight it gives better results, see:

git's diff-highlight:

diff-highlight

diffr:

diffr

Once installed, making use of it is similar to that of diff-highlight, so for example execute these two commands to add diffr to your global config:

$ git config --global core.pager 'diffr | less'
$ git config --global interactive.difffilter diffr
7
  • 7
    Can confirm that diffr provides a much better within-line change indication than diff-highlight. That screenshot didn't sell me but the first thing i was trying to do, the entire 500 character block of text was highlighted by diff-highlight while the only actual changes— two extra " \n" —were highlighted by diffr. Thanks Hi-Angel!
    – mlncn
    Commented Jun 9, 2021 at 11:59
  • @mlncn you are welcome! Check out also options diffr supports, you might be interested in making use of some of them. E.g. I personally use --line-numbers aligned option.
    – Hi-Angel
    Commented Jun 9, 2021 at 12:32
  • 3
    See GitHub page (diffr) for installation. For Ubuntu, I installed with $ cargo install diffr, and modified .gitconfig as stated in Integration with git.
    – Burak
    Commented Jul 12, 2021 at 13:41
  • Adding the [pager] section to the ~/.gitconfig file did not work with my Ubuntu 20.04 system. Then I installed diffr and followed instructions in the diffr GitHub repo. Now my git command works flawlessly with diffr. Thank you @Burak
    – Tom Nguyen
    Commented Jul 29, 2021 at 3:08
  • @TomNguyen by any chance, perhaps do you have another [pager] section elsewhere in the .gitconfig? I am not sure what else could go wrong. I just tested my answer by renaming the older ~/.gitconfig, and creating a new one with the sole content from the post, and it works for me. Although I'm testing on Archlinux, but that shouldn't really matter, the behavior related to git configs should be the same between distros.
    – Hi-Angel
    Commented Jul 29, 2021 at 20:28
18

You could use the --word-diff[=<mode>] option to make it easier to see which words have changed within a line. This is described in the man page as

Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see --word-diff-regex below. The <mode> defaults to plain, and must be one of:

  • color – Highlight changed words using only colors. Implies --color.

  • plain – Show words as [-removed-] and {+added+}. Makes no attempts to escape the delimiters if they appear in the input, so the output may be ambiguous.

  • porcelain – Use a special line-based format intended for script consumption. Added/removed/unchanged runs are printed in the usual unified diff format, starting with a +/-/` ` character at the beginning of the line and extending to the end of the line. Newlines in the input are represented by a tilde ~ on a line of its own.

  • none – Disable word diff again.

Note that despite the name of the first mode, color is used to highlight the changed parts in all modes if enabled.

1
  • I tried adding a [pager] section in my ~/.gitconfig file containing the line ` diff = git diff --word-diff=plain --color -- "$@" | less` but typing git diff myFile prints the differences for ALL the files I've changed inside my repo. Do you have some ~/.gitconfig idea to automate a git diff --word-diff=plain --color -- "$@" when I type git diff anyFile ?
    – SebMa
    Commented Dec 9, 2021 at 20:33
8
+200

Delta is a modern alternative to the postprocessing tools in other answers.

It is highly configurable (with emulation modes for diff-highlight and diff-so-fancy) and includes many features not found in other tools: side-by-side views, syntax highlighting, and coloring of merge conflicts and git blame output.

The Delta documentation also has an overview of related projects that mentions a few more ad-hoc tools that can produce similar output.

Delta diff formatting example

0
3

To expand on @Hi-Angel answer, we could use a wrapper script to find the diff-highlight contrib script and place it in $PATH. Then use the wrapper script in your .gitconfig.

The script will try to find contrib/diff-highlight. We will name the script wrapper diff-highlight and place it in $PATH:

#!/bin/sh
# diff-highlight locates the diff-highlight script in the git source tree and
# runs it.
# https://github.com/git/git/blob/master/contrib/diff-highlight

hilite=

for prefix in /usr/share /usr/local/share; do
    # First try git-core directory
    if [ -f "$prefix/git-core/contrib/diff-highlight" ]; then
        hilite="/usr/share/git-core/contrib/diff-highlight"
    elif [ -d "$prefix/git-core/contrib/diff-highlight" ]; then
        hilite="$prefix/git-core/contrib/diff-highlight/diff-highlight"
        # Then try git contrib directory
    elif [ -f "$prefix/git/contrib/diff-highlight" ]; then
        hilite="$prefix/git/contrib/diff-highlight"
    elif [ -d "$prefix/git/contrib/diff-highlight" ]; then
        hilite="$prefix/git/contrib/diff-highlight/diff-highlight"
        # Try git directory
    elif [ -f "$prefix/git/diff-highlight" ]; then
        hilite="$prefix/git/diff-highlight"
    elif [ -d "$prefix/git/diff-highlight" ]; then
        hilite="$prefix/git/diff-highlight/diff-highlight"
        # Then try doc directory
    elif [ -f "$prefix/doc/git/contrib/diff-highlight" ]; then
        hilite="$prefix/doc/git/contrib/diff-highlight"
    elif [ -d "$prefix/doc/git/contrib/diff-highlight" ]; then
        hilite="$prefix/doc/git/contrib/diff-highlight/diff-highlight"
    fi
    if [ -n "$hilite" ]; then
        break
    fi
done

if [ -x "$hilite" ]; then
    exec "$hilite" "$@"
elif command -v perl >/dev/null 2>&1; then
    perl "$hilite" "$@"
else
    cat
fi

You can also find this script in my dotfiles.

Then change the git pager to use the wrapper script:

$ git config --global core.pager 'diff-highlight | less'
$ git config --global interactive.diffFilter 'diff-highlight'
1

Sometimes just changing the coloring engine of git helps so much. Try diff-so-fancy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.