Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created July 13, 2012 10:39
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dansimau/3104183 to your computer and use it in GitHub Desktop.
Save dansimau/3104183 to your computer and use it in GitHub Desktop.
Enable git-style colour output in regular diff on Mac OS X

Enable git-style colour output in regular diff

Mac OS X

  1. Install colordiff using Homebrew:

     brew install colordiff
    
  2. Add function to your ~/.bash_profile:

     cat <<EOF >>~/.bash_profile
    
     function diff {
         colordiff -u "$@" | less -RF
     }
     EOF
    
  3. Change default output colours to match git:

     cat <<EOF >~/.colordiffrc
     newtext=green
     oldtext=red
     diffstuff=cyan
     EOF
    
  4. Reload:

     . ~/.bash_profile
    
  5. Profit.


Notes on switches:

  • -u in diff means: unified diff
  • -R in less means: show raw escape codes (so we can see the colour)
  • -F in less means: quit if entire output fits on one screen
@gf3
Copy link

gf3 commented Sep 24, 2014

this is super old, but you can actually just use git diff on regular old files outside of a repo

@LeviSchuck
Copy link

Another comment many years down the line:

This doesn't work if you have really short diffs, as less will automatically quit out and not leave a history when the length can be displayed on screen.

If you are willing to scroll, I'd just make a new function (not replacing diff directly).

function cdiff {
        colordiff -u "$@"
}

Also @gf3's comment is not true. git diff is not made to compare multiple files and will just show a blank screen with (END) at the top.
Review: http://stackoverflow.com/questions/13964328/git-diff-two-files-on-same-branch-same-commit

@gregbrownlumira
Copy link

@LeviSchuck:

git diff does work on files outside a repo. Output of git diff when run with no arguments:

Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>

When path values are specified, the command diffs the files as expected.

@emilianbold
Copy link

git diff does work if one of the file is within the repo and one external.

@ebkalderon
Copy link

Just checked git diff with two files not belonging to any a Git repo (current directory is not a repo tree either), and it works for me. This is with Git 2.19.1 on macOS.

@harrybiddle
Copy link

One thing that colordiff supports and git diff doesn't is a feature I use very often: diff'ing the output of commands:

$ colordiff <(echo hello, world) <(echo goodbye, cruel world)
1c1
< hello, world
---
> goodbye, cruel world

whereas

$ git diff <(echo hello, world) <(echo goodbye, cruel world)
error: /dev/fd/63: unsupported file type
fatal: cannot hash /dev/fd/63

@johnpancoast
Copy link

johnpancoast commented Sep 19, 2021

I suppose it's not related to git but if you want similar diffs and have VIM there is another solution shown here.

diff file1 file2 | vim -R -
-or-
diff file1 file2 | view -.

The view command is same as vim -R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment