25

If I execute git diff I see the whole line in red color.

Is there a way to highlight the change in the line?

I often have a diff where just a single line got changed.

Highlighting the change in the line would make git diff more convenient for me.

git-diff-whole-line-red

git version 2.32.0

This should work on the command-line (no GUI).

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Similar question: https://stackoverflow.com/questions/8800578/how-to-colorize-diff-on-the-command-line – guettli Jan 20 '22 at 12:53
  • https://stackoverflow.com/questions/5326008/highlight-changed-lines-and-changed-bytes-in-each-changed-line + https://github.com/git/git/tree/master/contrib/diff-highlight – terrorrussia-keeps-killing Jan 20 '22 at 13:29

3 Answers3

39

Use the --word-diff option for git diff

For example, git diff --word-diff=color:

git-diff-word-diff-color

and git diff --word-diff=plain:

git-diff-word-diff-plain

There's also --word-diff-regex=<regex>

See git help diff for more info.

Inigo
  • 12,186
  • 5
  • 41
  • 70
  • 4
    If you want minimal highlighting, use `--word-diff-regex=.`, which is sometimes clearer (when changs that qualifiy as a "word" get noticeably longer than the "levels" change above, that's clear enough as-is). – jthill Jan 18 '22 at 01:06
7

If you're willing to use an extra tool, you may like diff-so-fancy and/or delta.

  • I used diff-so-fancy for years, and it's a great tool.

  • I switched to delta due to syntax highlight and the detection of multiple changes in a single line.

Michael
  • 8,362
  • 6
  • 61
  • 88
Pedro Rodrigues
  • 1,662
  • 15
  • 19
1

Use colordiff tool colordiff so you can use with the git diff to see all minor changes with color

yum install colordiff

# enter yout git repository
git diff --word-diff=color

for example in my directory the tool output the image below

enter image description here:

guettli
  • 25,042
  • 81
  • 346
  • 663
Franz Kurt
  • 1,020
  • 2
  • 14
  • 14
  • While `colordiff` is useful for non-Git usage, the command `git diff --word-diff=color` operates just with Git’s own resources, it does not use `colordiff`. – Melebius Mar 23 '23 at 14:29