Understanding .gitignore with git check-ignore
31 May 2021 in TIL
Another day, another obscure yet very useful git
command! This time, it’s git check-ignore
which helps us to understand why a file is being ignored, or why it’s not if you expect it to be.
To use the function, call git check-ignore -v /path/to/file
. The -v
flag tells git
to also output the matching rule in addition to the file path matched.
Here’s an example where the path matches exactly:
bash
❯ git check-ignore -v node_modules.gitignore:41:node_modules/ node_modules
And here’s an example where it matches a wildcard:
bash
❯ git check-ignore -v debug.log.gitignore:3:*.log debug.log
If you’ve got specific files to exclude from a pattern, it can show those too. If your .gitignore
contains *.log
as above and you add !debug.log
, the debug.log
file will be added as we have added a negation with !
to say that it should not be ignored. git check-ignore
can show this too:
bash
❯ git check-ignore -v debug.log.gitignore:4:!debug.log debug.log