Git reports invalid patterns in .gitignore when using incorrect syntax, unsupported regex, or invalid special characters. This prevents proper file exclusion and can cause unexpected tracking behavior.
Git uses glob-style pattern matching in .gitignore files to determine which files should be excluded from version control. When Git encounters a pattern that doesn't conform to its syntax rules, it treats it as invalid and may ignore it entirely. Unlike regular expressions, .gitignore uses a specific glob pattern syntax with limited metacharacters. Common issues include trailing backslashes (which create invalid patterns that never match), using regex syntax like parentheses or pipe symbols (which are treated as literal characters), and incorrect wildcard usage. Git will sometimes silently ignore invalid patterns, leading to confusion when files aren't being ignored as expected. In other cases, Git may issue warnings about problematic patterns during operations.
Check your .gitignore file for common syntax errors. Make sure there are no trailing backslashes and that each pattern is on its own line:
# Check for syntax issues in .gitignore
cat .gitignore
# Look for:
# - Trailing backslashes at end of lines
# - Regex syntax like (pattern1|pattern2)
# - Incorrect wildcard usageValid glob pattern examples:
# Match specific file
debug.log
# Match all .log files in any directory
*.log
# Match logs directory recursively
**/logs/
# Match all .log files in logs and subdirectories
logs/**/*.log
# Match at specific depth
a/**/bUse git check-ignore to debug which patterns are matching specific files:
# Test if a file is ignored and why
git check-ignore -v path/to/file.txt
# Test multiple files
git check-ignore -v file1.txt file2.log directory/
# Check all files in a directory
find . -type f | git check-ignore --stdin -vThe output shows:
- The .gitignore file location
- Line number of matching pattern
- The pattern itself
- The file being tested
Replace invalid patterns with correct glob syntax:
Trailing backslashes (INVALID):
# ❌ Invalid - never matches
path/to/dir\
# ✅ Correct
path/to/dir/Using regex instead of globs (INVALID):
# ❌ Invalid - parentheses treated as literal characters
(debug|test)\.log
# ✅ Correct - use separate patterns
debug.log
test.logSingle vs double asterisk:
# ❌ Only matches direct children
logs/*.log
# ✅ Matches recursively in all subdirectories
logs/**/*.logDirectory matching:
# Matches both file and directory named 'temp'
temp
# Matches only directory named 'temp'
temp/If files were tracked before adding .gitignore patterns, you must untrack them:
# Remove all files from Git index (staging area)
git rm -r --cached .
# Re-add all files (respecting new .gitignore rules)
git add .
# Verify what will be committed
git status
# Commit the changes
git commit -m "Fix .gitignore - stop tracking previously ignored files"This removes files from Git tracking while preserving them in your working directory.
Ensure .gitignore has proper encoding and line endings:
# Check file encoding
file .gitignore
# Should show: .gitignore: ASCII text
# Convert line endings if needed (Linux/macOS)
dos2unix .gitignore
# Or use sed
sed -i 's/
$//' .gitignore
# Check for hidden characters
cat -A .gitignoreHidden characters, BOM markers, or CRLF line endings can cause patterns to fail silently.
Understanding Pattern Precedence
Git evaluates .gitignore patterns in order, with later patterns overriding earlier ones. Negation patterns (!) can re-include files, but only if their parent directory isn't excluded. Due to performance optimizations, Git doesn't traverse excluded directories, making it impossible to re-include files within them.
Global vs Local Configuration
Git checks multiple ignore sources in this order:
1. Patterns from the command line
2. .gitignore in the same directory as the file
3. .gitignore in parent directories (up to repository root)
4. $GIT_DIR/info/exclude
5. Global excludes file (core.excludesFile, typically ~/.config/git/ignore)
Conflicts between these sources can cause unexpected behavior. Check global configuration with:
git config --get core.excludesFile
cat $(git config --get core.excludesFile)Case Sensitivity Considerations
Git is case-sensitive, but file systems vary. Windows and macOS use case-insensitive file systems by default, which can create confusion. Pattern File.txt won't match file.txt on Linux but will on Windows. Consider using case-insensitive patterns when working across platforms.
Performance with Complex Patterns
Avoid overly broad patterns like **/node_modules/**/* when node_modules/ suffices. Git optimizes directory-level exclusions better than deep file-level patterns. Use git gc and git prune periodically to clean up ignored files that were previously tracked.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server