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 --check catches whitespace errors and conflict markers

Git
~/tips/tip-git-diff-check-whitespace

git diff --check

Warns about trailing whitespace and leftover conflict markers in your changes.

Leftover conflict markers or trailing spaces in a commit are the kind of thing someone spots in review. git diff --check finds them first.

The docs say it warns if your changes introduce conflict markers or whitespace errors, and that it exits with a non-zero status if it finds problems. By default a whitespace error is trailing whitespace, or a space immediately followed by a tab inside the indent. The core.whitespace setting changes what counts. Run it on what you changed so far, or on what you are about to commit:

git diff --check
git diff --cached --check

With a file that has two bad lines the output looks like this:

a.conf:2: trailing whitespace.
+host = example.com  
a.conf:3: leftover conflict marker

The first form only looks at unstaged changes. After git add it prints nothing and exits 0, which is why the second form is the one to run before a commit.

Because of the exit code, you can use it as a gate in a pre-commit hook or script:

git diff --cached --check || exit 1

Caveats: the docs say it is not compatible with --exit-code, so use one or the other. It also only checks lines you added or changed. Old trailing whitespace elsewhere in the file stays quiet. A line of exactly seven equals signs, such as a Markdown heading underline, is reported as a conflict marker too, so expect the odd false alarm.

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