How to improve git's diff highlighting?

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


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, i.e. edit ~/.gitconfig, and add to the [pager] section following:

[pager]
    log  = diffr | less
    show = diffr | less
    diff = diffr | less

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.