A file refuses to show up in git status and you have no clue why. I find that one of the more annoying git puzzles, because the rule can sit in the project’s .gitignore, in a .gitignore three folders down, in .git/info/exclude or in your global excludes file. You don’t have to go looking. Git will tell you.
git check-ignore -v path/to/file
With -v it prints where the rule lives: the file, the line number and the pattern, followed by the path you asked about. Without -v you only get the path back, which confirms that it’s ignored but not why.
Say your local settings file never gets committed:
$ git check-ignore -v config/local.env
.gitignore:12:*.env config/local.env
So the culprit is the *.env pattern on line 12 of the .gitignore in the repository root. When the rule comes from your global excludes file (core.excludesFile), the source is printed as an absolute path, and that’s exactly the case where I’d never think to look.
You can pass several paths at once. Add --non-matching (or -n) next to -v and git also lists the paths that no rule matched, with empty fields in front, so you see the whole picture in one go.
The catch: by default it only looks at files git doesn’t track yet. A tracked file is never ignored, whatever .gitignore says, so check-ignore prints nothing for it. Add --no-index when you want to know which rule would match anyway, for instance for a file someone once added with git add -f. And if the pattern in the output starts with !, that’s a negation: the file matched a rule that un-ignores it.
Further reading: git-check-ignore documentation, gitignore documentation
Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more