51

I need to diff two files (not two versions of the same file, they are however tracked by git, but that is unrelated) and I would like some colored output, how can I achieve that?

$ diff file_1 file_2

1,9d0
< <script ... >
<     // more code
< </script>

$ 

Above code shows me the difference between those files, however without any colors. For longer diffs that is hard to read.


Alternatively, is there a way for git (with which I do have nice color output) to diff two different files (not changes to a file)?

OSX (10.7.5)

miphe
  • 989
  • 2
  • 7
  • 20

6 Answers6

58

When diffing files I almost always use vim:

vim -d file_1 file_2

It not only uses colours, it lines up the files so it's easier to see lines added/removed.

Philip Kearns
  • 1,055
  • 7
  • 11
48

Perl has a a lackluster colordiff wrapper for diff, but I prefer grc (generic colorizer).

With grc (generic colorizer), you can write your own wrappers for different types of commands or inputs (if you like that sort of thing).

Below, grc is running against /var/log/syslog (in the config, this file is set to a certain color scheme), where it highlights processes, pids, IPs and "connect"s.

Of course, it is recommended to use an alias so you don't forget:

alias diff="/usr/bin/grc /usr/bin/diff"

grc running against syslog


If you have git, you may just want to use that, which allows very robust diffing, even across branches.

git diff master:cogs/foo.txt branch:widgets/bar.txt

You do not have to use git diff within a repository, you can use it for just regular files.enter image description here

git diff old.txt new.txt

As always, you can alias diff for ease of use.

alias diff="git diff"
jnovack
  • 1,396
  • 8
  • 19
  • 8
    yay for git diff – chrismarx Feb 04 '15 at 19:33
  • 12
    ``git diff`` should be at the top of your answer! +1 for pointing out that it works even outside of a repository. – Lucio Paiva Feb 07 '15 at 18:26
  • 7
    'git diff' does not work on generic files so aliasing diff to be 'git diff' can be harmful – Anton Chikin Apr 23 '15 at 23:13
  • 1
    This doesn't work for me... `echo one > foo; echo two > bar; git diff foo bar` produces no output, while `diff foo bar` produces `1c1 < one --- > two ` (with proper formatting, of course) – LarsR Nov 23 '16 at 08:04
  • 1
    `git diff` doesn't work for e.g. pipes – Piotr Findeisen Jan 24 '18 at 15:49
  • `git diff` does not work if both files happen to be in the same repository and currently checked-out. – matvore Nov 20 '20 at 04:07
  • `git diff` also has different command-line flags and different behavior, e.g. `git diff ` diffs two directories recursively, while `diff ` is not recursive and requires the `-r` flag (which `git diff` doesn't have) for a recursive diff. I'm not convinced aliasing is such a hot idea. – David Moles Mar 18 '21 at 21:37
  • `git diff old.txt new.txt` did the trick. Incredible tool. Thanks – Francisco Dec 07 '22 at 22:29
12

To build on the approved answer: grc works great for this. It is installable with brew and colorizes a number of terminal commands out of the box, diff being one of them. So...

brew install grc

...installs grc to your system. Then you need to set up your aliases, the brew caveat provides a solution. Simply add the following line to your .bashrc or similar.

source "`brew --prefix`/etc/grc.bashrc"

This will currently add the following aliases:

alias colourify="$GRC -es --colour=auto"
alias configure='colourify ./configure'
alias diff='colourify diff'
alias make='colourify make'
alias gcc='colourify gcc'
alias g++='colourify g++'
alias as='colourify as'
alias gas='colourify gas'
alias ld='colourify ld'
alias netstat='colourify netstat'
alias ping='colourify ping'
alias traceroute='colourify /usr/sbin/traceroute'
alias head='colourify head'
alias tail='colourify tail'
alias dig='colourify dig'
alias mount='colourify mount'
alias ps='colourify ps'
alias mtr='colourify mtr'
alias df='colourify df'
10

Homebrew has a formula for GNU diffutils, including a diff command with support for color.

$ brew install diffutils
$ /usr/local/bin/diff --color -r -c foo bar

color output

David Moles
  • 467
  • 2
  • 6
  • 19
  • 2
    This is the best answer, actually - simply replaces the crippled MacOS version with the proper GNU equivalent, and magically, things work as they're supposed to be. – Moritz Friedrich Aug 05 '21 at 09:50
  • I also like this answer the best. But confess a fondness for the `vim -d` option, previously unknown to me. – MXWest Aug 04 '22 at 20:29
  • Thanks for the tip re: `diffutils`! I don't know if others will encounter this, but `brew` installed the diff utility here on my machine: `/opt/homebrew/bin/diff`. – jgpawletko Feb 28 '23 at 22:00
9

You can get git to diff two different files:

git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
Colin Pickard
  • 8,579
  • 3
  • 31
  • 38
0

If you have Visual Studio Code (VS Code) installed and on your PATH, you can run code --diff file1 file2.

It provides a really nice side by side comparison with word diff, syntax highlighting, editing code while running the diffs, etc.

VS Code has an option to add itself to your PATH (uses a symlink actually) that you can find here.