🔀 Git: Fix git grep Missing a Match Inside a File Because the Repository’s .gitattributes Marks That Path as Binary
git grep, like diff and most other text-oriented Git plumbing, skips any path Git’s attribute system considers binary — determined either by content-sniffing or, more commonly in a configured repository, by an explicit -diff/binary attribute set in .gitattributes for a path pattern. A plain-text file that happens to match a broad glob meant for genuinely binary files — a *.dat pattern intended for compiled fixtures that also happens to match a text config.dat — gets silently treated as binary by every text-based Git command, so git grep simply never searches its content and reports no match, with nothing telling you the file was skipped rather than searched and found clean.
🔎 The Problem
# .gitattributes *.dat binary # config.dat is actually plain text, but matches the glob anyway: $ git grep "legacy_api_key" # (no results at all, even though config.dat contains the string) $ git check-attr -a -- config.dat config.dat: diff: unset config.dat: binary: set # <- this is why git grep silently skipped it
✅ Fix: Check .gitattributes Before Trusting a Clean Grep
- Run
git check-attr -a -- <path>on a suspiciously grep-invisible file to see exactly which.gitattributesrule marks it binary, before assuming the content itself just doesn’t contain your search term. - Narrow an overly broad glob in
.gitattributes(like*.dat binary) to the specific directory or more specific pattern that actually contains binary fixtures, rather than a wildcard that can catch a legitimate text file by coincidence. - Pass
git grep -a(treat all files as text) when you specifically need to search across files you suspect might be misclassified, as a one-off override without changing the repository’s.gitattributesitself.
🚨 Why This Is Dangerous
- A security or configuration audit that greps the whole repository for a leaked credential or deprecated setting can report a clean result while a matching file sits right there untouched, simply because it was never actually searched — and a clean grep result is exactly the kind of thing nobody double-checks.
git grep doesn’t report a file as clean — it reports a file as skipped, and a .gitattributes rule is the only thing standing between those two very different outcomes.

