Diffing files using git
Today I had to find a fast way to compare the contents of two files to see how they compare to each other.
Both of them were well over a thousand lines long therefore mostly similar and I really didn’t want to go through them manually.
My first instinct was to use the git diff
command, but they weren’t in a repo yet.
Great… I really didn’t want to go through the hassle of setting up a repo and copying files around etc.
Typically I try to find solutions that already leverage existing tools on my machine.
After a quick search, I turns out I can just pass a --no-index
flag to the git diff
command.
The command then looks as follows:
git diff [<options>] --no-index [--] <path> <path>
Great, right? This means I don’t need to set up a new git repository for this.
You can omit the
--no-index
option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.1
Additionally (especially when comparing larger prose files) I can pass in a --word-diff
flag so it will even highlight words within lines.
1 Example
Assuming the following two files:
Here’s to the crazy ones.The misfits.The rebels.The troublemakers.
Here’s to the ordinary people.The ones who fit in.The everyday folks.The rule-followers.`
and running
git diff --no-index --word-diff think_different.txt think_normal.txt
I get an output like this
diff --git a/think_different.txt b/think_normal.txtindex b75cfa6..05417e6 100644--- a/think_different.txt+++ b/think_normal.txt@@ -1,4 +1,4 @@Here’s to the [-crazy ones.-]{+ordinary people.+}The [-misfits.-]{+ones who fit in.+}The [-rebels.-]{+everyday folks.+}The [-troublemakers.-]{+rule-followers.+}