Glob and Pattern Matching
What
Git does not require you to write out exact, individual file paths inside a .gitignore file. Instead, it supports powerful Glob Pattern Matching options (similar to shell expansion) to ignore entire groups of files dynamically.
How
You can utilize multiple pattern matching mechanisms inside a .gitignore file:
-
Wildcards (
*): Matches any sequence of characters except for directories (/):# Ignore all plain text files in any directory *.txt -
Root Anchoring (
/): Anchors the pattern specifically to the directory where that.gitignoreresides:# Ignore 'main.py' at the root, but NOT at 'src/main.py' /main.py -
Negation (
!): Negates a previous ignoring rule, forcing Git to track a matching file:# Ignore all text files, but explicitly track 'important.txt' *.txt !important.txt -
Comments (
#): Add descriptions to make patterns understandable:# Ignore temporary logging folders logs/
Note: Order matters inside .gitignore files. Exclusions and negations are parsed sequentially from top to bottom. A negation pattern must appear after the ignore pattern it is overriding to take effect.
Why
Using pattern-based wildcards makes your repository robust. Instead of manually updating your .gitignore every time a developer adds a new local text log or temporary cache file, writing expressive globs like *.log or tmp/ automatically handles all future files of that type, keeping the repository clean hands-free.
Summary
Combine wildcards (*), root-anchors (/), and negations (!) in sequential order to dynamically scale how Git ignores local workspace files.