`.gitignore`
A .gitignore file specifies intentionally untracked files that Git should ignore. It does not stop tracking files already in the index or remove anything from commit history. This is particularly useful for excluding files that contain sensitive information, user-specific configurations, temporary files created by the development environment, or dependencies that can be installed via a package manager.
Syntax and Structure
A .gitignore file uses glob patterns to match file names. Here's how it works:
- Blank lines are ignored, which can be used for spacing and organization.
- Lines starting with
#are treated as comments. - Standard glob patterns are used for matching files or paths. These include:
*matches zero or more characters except/.?matches exactly one character except/.[...]matches any character inside the brackets.**/matches any directories recursively.
Common Patterns
# Ignore all .log files
*.log
# Ignore a specific file
secret.txt
# Ignore all files in a specific directory
tmp/*
# Ignore all .txt files in a specific directory
docs/*.txt
# Ignore a directory and everything inside it
node_modules/
# Ignore all .pdf files in any directory
**/*.pdf
Best Practices
- Shared, nested, and personal rules: Commit repository-wide rules in the root
.gitignore; nested.gitignorefiles can add or override rules within directories Git traverses. If a parent directory is excluded, its nested rules cannot re-include files until that parent is unignored. Keep repository-local personal patterns in.git/info/exclude, or configurecore.excludesFilefor personal patterns shared across repositories. - Commit
.gitignoreEarly: It's a good practice to add and commit your.gitignorefile as early as possible in your project to prevent accidental inclusion of unwanted files. - Sensitive Data: If a credential was committed, rotate or revoke it first. Adding the path to
.gitignoredoes not remove existing history. Usegit filter-repo, BFG Repo-Cleaner, or the hosting provider's documented history-removal procedure, then coordinate the rewritten history with every collaborator. - Templates: Many projects share similar
.gitignoreneeds. Websites like gitignore.io offer templates for various project types, making it easy to start with a comprehensive.gitignorefile.
Example .gitignore for a Java Project
# Compiled class files
*.class
# Build directories
target/
out/
# Package Files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# Maven
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# IntelliJ IDEA
.idea/
*.iml
*.iws
# Eclipse
.classpath
.project
.settings/
# Mac OS folder attributes
.DS_Store
In conclusion, a properly configured .gitignore file is essential for maintaining a clean and secure repository. It helps to minimize clutter and prevent the accidental commit of irrelevant or sensitive files.
Compare the relevant language and editor files in GitHub’s gitignore collection with your project. Read why each pattern is needed before copying it, and test uncertain matches with git check-ignore -v. A template is a starting point; it does not know which generated files your project intentionally tracks.