Complete .gitignore Rule Syntax & Wildcard Reference
Understanding gitignore wildcards and pattern matching helps prevent sensitive API keys, build artifacts, and vendor binaries from ever polluting your Git repository.
Ignore a Directory & Subdirectories
How to git ignore a directory, git ignore folder and contents, or git ignore folder and subfolders:
build/
# Matches all files in directory
logs/*
# Matches subdirectory anywhere in tree
**/temp/
Covers gitignore directory and subdirectories, gitignore all files in directory, and gitignore next folder.
Git Ignore Exception (!)
To create a git ignore exception or gitignore exception (e.g. gitignore everything except a specific file):
*.log
# But keep important.log
!important.log
Note: You cannot re-include a file if its parent directory is excluded.
Local Gitignore vs Git Global Ignore
Depending on your workflow scope, Git provides multiple levels of exclusion:
- Repository .gitignore: Committed to the repository, sharing exclusions across all team members. You can have a root file or multiple gitignore files in each subdirectory gitignore (e.g., git ignore subdirectory).
- Local gitignore (.git/info/exclude): Excludes private files only on your local machine without committing anything to remote repositories.
- Git global ignore: Configured once across your entire computer using
git config --global core.excludesfile ~/.gitignore_globalfor OS files like macos gitignore (.DS_Store) or IDE configs (vscode git ignore, visual studio git ignore, or gitignore visual studio).
Gitignore Comments and Wildcard Syntax
To document your rules, use standard gitignore comments starting with a hashtag symbol (#). A single asterisk git ignore wildcard (*) matches zero or more characters within a folder name, while double asterisks (**) span recursively across multiple folder levels.