Skip to main content

How to See Why Git Ignores a File with git check-ignore

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When a file never shows up in git status, a .gitignore rule is usually hiding it, but which one? git check-ignore -v names the file and the line that ignores a path. This guide checks a log file, a file in a build folder and a README, with real output from Git 2.56.

Syntax​

Syntax
git check-ignore -v <path> [<path> ...]
PartWhat it means
<path>A file or folder to check. It doesn't have to exist
-vAlso prints the rule that matched: the file, its line number and the pattern

Git's check-ignore documentation: for each path, it checks "whether the file is excluded by .gitignore (or other input files to the exclude mechanism) and output the path if it is excluded." And for -v: "Instead of printing the paths that are excluded, for each path that matches an exclude pattern, print the exclude pattern together with the path."

The Example​

A repository called git-demo whose .gitignore has two lines:

.gitignore
*.log
build/

The folder holds README.md, app.log and build/output.txt.

Prerequisites​

  • Git. This guide uses Git 2.56.1.

Step 1: Check Three Paths​

Terminal
git check-ignore -v app.log
git check-ignore -v build/output.txt
git check-ignore -v README.md
Output
.gitignore:1:*.log app.log
.gitignore:2:build/ build/output.txt

A terminal running git check-ignore -v on app.log, build/output.txt and README.md: line 1, *.log, ignores app.log, line 2, build/, ignores build/output.txt, and README.md prints nothing.

Each line reads: the file with the rule, the rule's line number, the pattern, then the path it matched. app.log is ignored by line 1, *.log, and build/output.txt by line 2, build/. README.md printed nothing: no rule ignores it.

Step 2: Read the Answer from the Exit Status​

Commands that print nothing still answer with their exit status, which echo $? prints right after:

Terminal
git check-ignore -v README.md
echo $?
Output
1

The documentation lists the exit statuses: "0 One or more of the provided paths is ignored. 1 None of the provided paths are ignored. 128 A fatal error was encountered." 1: README.md isn't ignored.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out -vYou learn that the path is ignored, but not which rule does itAdd -v
Expecting output for a file that isn't ignoredThere is noneRead the exit status: 1 means not ignored

Troubleshooting​

SymptomLikely causeFix
A new file never appears in git statusA .gitignore rule matches itgit check-ignore -v <file> names the rule

FAQ​

Does the file have to exist?​

No. Git checks the path against the rules: git check-ignore -v nothere.log, for a file that doesn't exist, printed .gitignore:1:*.log and the name.

Can I check a whole folder?​

Yes. git check-ignore -v build printed .gitignore:2:build/ and build: line 2 ignores the whole folder.

Conclusion​

git check-ignore -v showed that line 1 of .gitignore, *.log, ignores app.log, and line 2, build/, ignores build/output.txt. README.md printed nothing, and the exit status, 1, confirmed that no rule ignores it.

Additional Resources​