How to get diff working like git-diff?

55,980

Solution 1

This will do the +/- rather than < and >.

diff -u file1 file2

Since GNU diffutils 3.4 the flag --color has been added. Combining both makes the following:

diff --color -u file1 file2

The flag --color also takes an argument, valid options are never, always, or auto. Useful when you want to be more explicit on what needs to be done.

Solution 2

You can also use git diff --no-index -- A B (via manpage).

Solution 3

  1. Install colordiff.

  2. Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):

    # be more git-like:
    plain=off
    newtext=darkgreen
    oldtext=darkred
    diffstuff=darkcyan
    
  3. Use colordiff -u file1 file2 for two files or colordiff -ruN path1 path2 for recursively comparing paths.

It's not exactly the same, but it's very close.

Solution 4

This is what I suggest and it's pretty close

diff -u FILE1 FILE2 | colordiff | less -R
  • colordiff: You'll have to install this
  • -R: this tells Less to show colors instead of the raw codes.

I ultimately used -w because I didn't want to see whitespace diffs.

diff -w -u FILE1 FILE2 | colordiff | less -R

Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your ~/.bashrc file too.

function gdiff () { diff -u $@ | colordiff | less -R; }

Solution 5

GNU diff has a --color option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside -u should be enough to mimic the output of git diff:

diff -u --color=always file1 file2 | less -r

--color must be always when used in a pipe, auto will turn off color in pipes.

I've only tried this with Git Bash on Windows, where less -R would only color the first line of a hunk. less -r fixed it for me in that case.

Share:
55,980

Related videos on Youtube

Mzzzzzz
Author by

Mzzzzzz

Updated on September 12, 2021

Comments

  • Mzzzzzz
    Mzzzzzz over 2 years

    I like the output formatting of git diff. The color and the +/- representation of changes between lines is easier to read than GNU diff.

    I can run git diff using --no-index flag outside of a git repo and it works fine. However, it appears to be missing the --exclude option for excluding files or subdirectories from a recursive diff.

    Is there a way to get the best of both worlds? (color options and +/- format of git diff and --exclude option of GNU diff).

    I've experimented with colordiff, but I still prefer the output format of git diff

    • Rudie
      Rudie about 9 years
      To make the blue for additions green, change newtext in /etc/colordiff. I think git uses green?
    • AJM
      AJM over 4 years
      I had never heard of the --no-index flag until now. I've just used it to compare the output from git show to the diff of two files - thanks for that!
  • Mzzzzzz
    Mzzzzzz over 13 years
    Cool, this combined with colordiff gets me close enough to what I want. Guess I need to scroll further down the man page next time... Thanks!
  • Emil Lundberg
    Emil Lundberg almost 11 years
    +1, but sadly this doesn't work if one of the files is a symlink.
  • tr33hous
    tr33hous over 9 years
    This is for the diff in git. He was asking for the diff program options
  • Ciprian Tomoiagă
    Ciprian Tomoiagă over 9 years
    To have a single bash function for this add to the .bashrc: function gdiff () { diff -u $@ | colordiff | less -R; }
  • J. Katzwinkel
    J. Katzwinkel over 9 years
    +1 This is very useful as it shows how to make git report where two tracked files A and B differ in comparison with each other instead of where each file has been modified relative to their last respective revision.
  • kkm
    kkm almost 9 years
    @EmilLundberg: works for me with symlinks in git 1.9.1 on Linux. I do not know whether earlier versions are broken.
  • Samuel Lampa
    Samuel Lampa over 8 years
    A simple way to get colorization with diff -u, is also to pipe the output to tig, the commandline git repo viewer: diff -u file1 file2 | tig.
  • Ken Williams
    Ken Williams almost 8 years
    git diff --no-index is great, but as the OP pointed out, it lacks the --exclude flag, so it's often of very limited usefulness.
  • iBug
    iBug almost 6 years
    Install colordiff from your apt/yum/pacman repository and use it.
  • Pat Myron
    Pat Myron over 4 years
    Needed to enable Extra Packages for Enterprise Linux (EPEL) on Amazon Linux to install colordiff: docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
  • Syrtis Major
    Syrtis Major over 4 years
    Besides colordiff, you can also get color with vim by defining cdiff() { diff -u $@ | vim -R -; }.
  • Adam Stewart
    Adam Stewart over 4 years
    Unfortunately this doesn't work on macOS, the BSD diff uses -u for a different option.
  • Bhupesh Varshney
    Bhupesh Varshney over 3 years
    Adding --color=always will enable colors
  • Kaiwen Sun
    Kaiwen Sun over 2 years
    For those who would like to install/upgrade the latest version of diff on Mac, you can run brew install diffutils, then reopen the terminal. -- from unix.stackexchange.com/a/592067/363687