Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

git diff Can Be Used Outside of a Git Repo and It Has Useful Features

git-diff-can-be-used-outside-of-a-git-repo-and-it-has-useful-features.jpg

This provides a cross OS compatibile diffing tool with great features like showing individual character diffs or directory diffs.

Quick Jump:

The GNU and OpenBSD (macOS) version of diff have different flags and features. Technically you can install the GNU version of diff on macOS but if you already have git installed you can use git diff even if you want to diff files not tracked by git!

Let’s say you want to diff 2 files not in git, you can do this:

git diff --no-index -- file_a file_b

That’s all there is to it, the --no-index flag lets you compare 2 paths and it lets git know they’re not a git commit or reference.

It gets even easier too. If the paths exist in a directory that’s not tracked by git you can simplify the command to git diff file_a file_b.

But, why use git diff when you can use diff? It depends. In portable scripts I will use diff but there are some cases where if I know git is installed and I want to do specific things, I’ll use git diff. Here’s a couple of examples.

# Useful Features of git diff vs diff

Besides being cross OS compatible, there’s a couple of useful features of git diff. This isn’t all of them but here’s 2 I use from time to time.

Directory Diffs

I added a few sample files in a directory:

/home/nick/src/tutorials/git-diff-tips
├── a
│   ├── 1
│   └── 2
└── b
    ├── 1
    ├── 2
    └── 3

2 directories, 5 files

Then I used different capitalization on words:

$ git diff ~/src/tutorials/git-diff/a ~/src/tutorials/git-diff/b

diff --git a/home/nick/src/tutorials/git-diff/a/1 b/home/nick/src/tutorials/git-diff/b/1
index 5626abf..3609f20 100644
--- a/home/nick/src/tutorials/git-diff/a/1
+++ b/home/nick/src/tutorials/git-diff/b/1
@@ -1 +1 @@
-one
+One
diff --git a/home/nick/src/tutorials/git-diff/a/2 b/home/nick/src/tutorials/git-diff/b/2
index f719efd..3b0086f 100644
--- a/home/nick/src/tutorials/git-diff/a/2
+++ b/home/nick/src/tutorials/git-diff/b/2
@@ -1 +1 @@
-two
+Two
diff --git a/home/nick/src/tutorials/git-diff/b/3 b/home/nick/src/tutorials/git-diff/b/3
new file mode 100644
index 0000000..b2cde18
--- /dev/null
+++ b/home/nick/src/tutorials/git-diff/b/3
@@ -0,0 +1 @@
+Three

Vim can also do directory diffs with a way to patch A to B or B to A.

Character Diffs

Sometimes it’s handy to see highlights for each character added or removed instead of seeing the whole line change due to 1 character.

# --color-words can take in a regex, in this case we say all characters are treated as words.
$ git diff --color-words=. file_a file_b

Here’s the results of running a similar command in an upgrade script I wrote for Plutus. My site’s diff syntax highlighter doesn’t show individual character diffs with color so here’s a screenshot. The new CSV header and new commas are colored up in green:

git-diff-characters.jpg

What’s neat is you can also do this in a git repo, such as git show --color-words=.. I really like how git offers a unified experience for presenting diffs across different commands that show diff outputs.

The video below demos most of the above.

# Demo Video

Timestamps

  • 0:17 – A quick aside on diff
  • 0:51 – Git diff files and directories not in a git repo
  • 2:21 - Simplifying our git diff command
  • 3:33 – Showing character differences instead of lines
  • 4:46 – Practical example of character diffs

What’s your favorite git diff tip? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments