76

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.

pigrammer
  • 2,603
  • 1
  • 11
  • 24
Vi.
  • 37,014
  • 18
  • 93
  • 148

5 Answers5

129
git diff --color-words --no-index old.txt new.txt
Flimm
  • 136,138
  • 45
  • 251
  • 267
Vi.
  • 37,014
  • 18
  • 93
  • 148
  • 8
    From the manpage: "If exactly two paths are given and at least one points outside the current repository, git diff will compare the two files / directories. This behavior can be forced by --no-index." (A lot of times you don't even need `--no-index`.) – Cascabel Apr 12 '11 at 15:59
  • 1
    At least in my git 1.9.3, the order should be `git diff --no-index --color-words` – PhML May 29 '14 at 13:16
  • 3
    In my git v1.9.3 order of `--no-index` and `--color-words` does not matter... (Actually I can't find a case now where it fails without `--no-index` at all). – Vi. May 29 '14 at 17:32
  • 1
    Works great. Maybe we can add an alias `alias diff="git diff --no-index"`? – Penghe Geng Aug 22 '18 at 14:31
8

According to a comment from Jefromi you can just use

git diff --color-words file1 file2

outside of git repositories too.

Thomas
  • 2,127
  • 1
  • 32
  • 45
  • 5
    git diff didn't work on two arbit files as mentioned above. (git version 1.7.4). Although,`git diff --color-words --no-index ` works, correct approach would be to use wdiff, which is intended for that purpose (http://www.gnu.org/software/wdiff/) – Manu Manjunath Dec 27 '12 at 05:56
  • 2
    @Manu No, `git diff` is intended for this purpose. `wdiff` is just a hack. Read your link sometime. – Zenexer Sep 17 '13 at 10:15
  • `git diff` is excellent, but the `wdiff` page noted above does not say that it's a hack. wdiff "is quite mature" according to its own documentation linked from that page: https://www.gnu.org/software/wdiff/manual/wdiff.html – Joshua Goldberg Dec 09 '19 at 19:18
3

Git version 1.9.1:

git diff --word-diff=color fileA fileB

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
1

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.

Pierre-Olivier Vares
  • 1,687
  • 15
  • 20
  • Why `git --no-index --color-words` fails? I used it (with earlier Git versions although). – Vi. Mar 20 '15 at 14:34
1

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 …

knittl
  • 246,190
  • 53
  • 318
  • 364
  • The question was about using it outside Git repository. Also it misses some things when `--color-words=always` instead of just `--color-words`. – Vi. Apr 12 '11 at 15:12
  • @vi: sorry, you only said »outside git«, it did not mention a repository anywhere. i thought it was about having the color in other applications beside git (when e.g. piped) – which is also »outside git«, in the sense of »not inside the default git toolchain/tool collection« – knittl Apr 12 '11 at 16:09