Search code examples
githighlighthighlightinggit-diff

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.


Solution

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