Debugging and understanding gitignore syntax

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the git category.

Last Updated: 2024-03-02

1. What do you do if can't figure out why something is being ignored by git?

You run this:

git check-ignore -v fileOrFolderName

In my case I learned that the surprising behavior I was seeing was due to a global config in ~/.gitignore_global

2. Gitignore syntax

The main modifiers to be aware of are:

target/ #…folder (due to the trailing /) recursively
target #…file OR folder named `target` recursively
/target #…file or folder named target in the _top-most_ directory (due to the leading /)
/target/ #…folder named target in the top-most directory (leading and trailing /)
!target # override the effect of a previous ignore and allow the file named `target`