How to get diff working like git-diff?
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
Install colordiff.
-
Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):
# be more git-like: plain=off newtext=darkgreen oldtext=darkred diffstuff=darkcyan
Use
colordiff -u file1 file2
for two files orcolordiff -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-
brew install colordiff
on my Mac. -
port install colordiff
on some Macs. -
sudo apt-get install colordiff
on Debian or Ubuntu - For other platforms, download the source from the main page or GitHub and follow the installation instructions
-
-
-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.
Related videos on Youtube
Mzzzzzz
Updated on September 12, 2021Comments
-
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 recursivediff
.Is there a way to get the best of both worlds? (color options and
+
/-
format ofgit diff
and--exclude
option of GNU diff).I've experimented with
colordiff
, but I still prefer the output format ofgit diff
-
Rudie about 9 yearsTo make the blue for additions green, change
newtext
in/etc/colordiff
. I think git uses green? -
AJM over 4 yearsI 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 over 13 yearsCool, 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 almost 11 years+1, but sadly this doesn't work if one of the files is a symlink.
-
tr33hous over 9 yearsThis is for the diff in
git
. He was asking for thediff
program options -
Ciprian Tomoiagă over 9 yearsTo have a single bash function for this add to the .bashrc:
function gdiff () { diff -u $@ | colordiff | less -R; }
-
J. Katzwinkel over 9 years+1 This is very useful as it shows how to make git report where two tracked files
A
andB
differ in comparison with each other instead of where each file has been modified relative to their last respective revision. -
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 over 8 yearsA 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 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 almost 6 yearsInstall
colordiff
from your apt/yum/pacman repository and use it. -
Pat Myron over 4 yearsNeeded to enable Extra Packages for Enterprise Linux (EPEL) on Amazon Linux to install
colordiff
: docs.aws.amazon.com/AWSEC2/latest/UserGuide/… -
Syrtis Major over 4 yearsBesides
colordiff
, you can also get color withvim
by definingcdiff() { diff -u $@ | vim -R -; }
. -
Adam Stewart over 4 yearsUnfortunately this doesn't work on macOS, the BSD diff uses
-u
for a different option. -
Bhupesh Varshney over 3 yearsAdding
--color=always
will enable colors -
Kaiwen Sun over 2 yearsFor 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