Is there a way to validate a .gitignore file so you quickly find doubles paths or paths that don't exist anymore? Normally when a project is small this isn't really necessary but with lots of branches and mergings of .gitignore files this can come in hand.
3 Answers
Not exactly.
One command which could come close would be git check-ignore
Choose a file you know is supposed to be ignored, and check the output of:
git check-ignore -v -- /path/to/ignored/file
You will see the rules of your .gitignore
which apply.
Update March 2016: Git 2.8 will add a new way to debug .gitignore
files and their rules.
In the context of allowing a sub-folder to not be ignored (even if its parent folder is ignored: see example here), I have found this gem by Thái Ngọc Duy (pclouds
):
dir.c
: support tracing exclude
man git
includes:
GIT_TRACE_EXCLUDE:
Enables trace messages that can help debugging
.gitignore
processing.
See 'GIT_TRACE
' for available trace output options.
With GIT_TRACE_EXCLUDE
set to 1, you will see (after a git status
) stderr debug messages like:
exclude: from ...
exclude: xxx => n/a
exclude: xxx vs. yyy at line z: => www
You can do a script to check it. I have made one for you there:
#!/bin/bash
set -o noglob
for file in `cat .gitignore | grep -v \#`
do
printf "$file"
find . -name "$file" | wc -l
done
it will display the rules followed by the number of match in the current directory and recursively. Example:
*.log 31
*.gz 0
*~ 42
*.swp 0
*.aux 33
*.pdf 51
*.out 7
*.toc 6
*.nav 1
*.snm 1
.DS_Store 0
You could restrict the output to the line containing 0
by piping into egrep "\b0\b"
if you want.

- 5,696
- 2
- 30
- 43
-
This assumes `find`'s globbing matches git's `.gitignore` globbing. Is that a safe assumption? – Lombard Sep 03 '20 at 12:22
-
To be fair, I haven't check. I also rarely use the fancy things like negation or double *. It should work just fine for the standard * wildcard though. – M'vy Sep 04 '20 at 17:07
In order to also validate git's ** pattern in paths I had to write a one-liner inspired by the script above:
find . -type f | git check-ignore -v --stdin | perl -pe 's,.*\.gitignore:,,; s,\t," "x100,e' | sort | uniq -w 100 -c | perl -pe 's, {100}.*,,'
Not exactly pretty but it works.

- 11
- 1