Diffs
TL;DR: I like the nice output from git diff better than the native diff command, but how do I convince git to let me do this for files which aren't source controlled?
I like to wrap this in a shell function I call fdiff
.
fdiff () {
if (( $# < 2 ))
then
echo "fdiff usage: pass two Files to see their DIFFerence"
echo "\x1b[100m"
echo "\t "
echo "\t > fdiff old.txt new.txt "
echo "\t \x1b[1m--- old = old.txt "
echo "\t \x1b[1m+++ new = new.txt\x1b[0m\x1b[100m "
echo "\t \x1b[36m@@ -1 +1 @@\x1b[39m "
echo "\t \x1b[31m-This is the old file "
echo "\t \x1b[32m+This is the new file "
echo "\t "
echo "\x1b[0m"
echo "accepts all options that git diff does, e.g. --word-diff -U0"
echo "\x1b[100m"
echo "\t "
echo "\t > fdiff old.txt new.txt --word-diff -U0 "
echo "\t \x1b[1m--- old = old.txt "
echo "\t \x1b[1m+++ new = new.txt\x1b[0m\x1b[100m "
echo "\t \x1b[36m@@ -1 +1 @@\x1b[39m "
echo "\t This is the \x1b[31m[-old-]\x1b[32m{+new+}\x1b[39m file "
echo "\t "
echo "\x1b[0m"
else
git --no-pager diff --no-index --color=always --src-prefix="old = " --dst-prefix="new = " ${@:3} -- $1 $2
fi
}
Here's a more minimal version
fdiff () {
git --no-pager diff --no-index ${@:3} -- $1 $2
}
Explanation
Breaking this down, the most important pieces are
git
git
command.diff
diff
command for git
.--no-index
-- $1 $2
Nice extra features.
--no-pager
--color=always
--src-prefix="old = "
--dst-prefix="new = "
${@:3}
You can read about all of the possible flags that can be passed to git diff
by consulting git help diff
.
Example Usage
Here we'll use the following two files, old.txt
and new.txt
.
Basic Usage
Running fdiff
on these files produces output like this.
Diffs on words
For day-to-day usage, I find --word-diff
or --color-words
the most useful.
Summarizing Diffs
If you just want an overview of diffs, you can use flags like --compact-summary
or
--numstat
Seeing Where Text Moved
If you're writing a longer document and you want to see where pieces have moved, you can use
--color-moved=plain
. Here I'm no longer using our previous example files.
Context
You can specify the number of lines of context (lines before and after) you want with the -U
flag.
So for 0 lines of context, use -U0
. For 10 lines, -U10
, etc. I often use this in
combination with --word-diff
.