AndyHunt.me

CV and personal site of Andy Hunt

You can use git diff on arbitrary files

Posted: Friday 05 March 2021 @ 16:16:26

Linux distributions often come with the diff utility installed by default. It's useful for comparing text files, but I find it's output hard to read. I know it's possible to get a git diff like output from it, but I always have to search for how.

Today I learned I can just use git diff, whose output I'm already familiar with reading.

To use it on arbitrary files, provide the --no-index option:

$ cat <<EOF > a.txt
foo
bar
baz
EOF

$ cat <<EOF > a.txt
foo
qux
baz
EOF

$ git diff --no-index a.txt b.txt

diff --git a/a.txt b/b.txt
index 86e041d..e3580b4 100644
--- a/a.txt
+++ b/b.txt
@@ -1,3 +1,3 @@
 foo
-bar
+qux
 baz