brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git diff with no-index compares two files outside a repository

Git
~/tips/tip-git-diff-no-index-any-files

git diff --no-index old.conf new.conf

Gives you git's colored diff for any two files or folders, even outside a repository.

I prefer git’s diff output over plain diff: colors, word diffs, moved code. With one option it works on any two files on disk.

git diff --no-index old.conf new.conf

Outside a repository, git switches to this mode by itself, but inside one you need --no-index, or git thinks you mean tracked files. You can compare two folders the same way, and all the usual options apply:

git diff --no-index --word-diff nginx.conf.bak nginx.conf
git diff --no-index --stat backup/ site/

I’d reach for this to compare a config file with its backup copy, or two exports of the same thing. A long line where only one value changed is much easier to read with --word-diff than with the line-based output of plain diff.

In a script, mind the exit code. With --no-index, git exits with 1 when the files differ and 0 when they’re the same, just like diff, so a set -e script stops at the first difference.

Further reading: git-diff documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link