How can I use `git diff --color-words` outside a Git repository?

GitColorsDiffWordWord Diff

Git Problem Overview


How can I get output like in git diff --color-words, but outside Git?

Closest thing is wdiff -t, but it underlines/inverts things instead of using green/red colours and does not allow specifying my whitespace regex.

Git Solutions


Solution 1 - Git

git diff --color-words --no-index old.txt new.txt

Solution 2 - Git

According to a comment from Jefromi you can just use

git diff --color-words file1 file2

outside of git repositories too.

Solution 3 - Git

Git version 1.9.1:

git diff --word-diff=color fileA fileB

Solution 4 - Git

you can say git diff --color=always --color-words, which will give you the color escape codes in the output. you are going to have some shell to interpret the color codes though …

Solution 5 - Git

If I'm inside a git repository (git v2.3.3) :

  • git diff --color-words doesn't work (no output)
  • git diff --no-index doesn't accept --color-words nor --color arguments

Using wdiff is possible, configured to use colors, rather than underlined :

wdiff -n \
  -w $'\033[30;31m' -x $'\033[0m' \
  -y $'\033[30;32m' -z $'\033[0m' \
  … | less -R

Source : https://www.gnu.org/software/wdiff/manual/html_node/wdiff-Examples.html (modified to use foreground colors rather than background colors)

Hope it helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVi.View Question on Stackoverflow
Solution 1 - GitVi.View Answer on Stackoverflow
Solution 2 - GitThomasView Answer on Stackoverflow
Solution 3 - GitJellicleCatView Answer on Stackoverflow
Solution 4 - GitknittlView Answer on Stackoverflow
Solution 5 - GitPierre-Olivier VaresView Answer on Stackoverflow