What is .gitignore?
A .gitignore file is a plain text file in a Git repository that specifies which files and directories Git should ignore. When you run git add or git status, Git checks the .gitignore rules and excludes matching files from being tracked.
Every project generates files that shouldn’t be committed to version control — build artifacts, dependency directories, IDE configuration files, compiled binaries, log files, and environment variables containing secrets. The .gitignore file keeps your repository clean and focused on source code.
How .gitignore Patterns Work
Each line in a .gitignore file specifies a pattern. Git uses glob-style matching:
*matches any number of characters (except/)?matches a single character**matches directories recursively!negates a pattern (un-ignores a previously ignored path)- Lines starting with
#are comments - Trailing
/matches directories only
# Ignore all .log files
*.log
# But keep error.log
!error.log
# Ignore the build directory
build/
# Ignore all .env files in any directory
**/.env
How to Use This Tool
- Select your languages and frameworks by checking the relevant boxes
- Select your operating system for OS-specific ignores
- Select your IDE to exclude editor configuration files
- Click “Generate” to build your
.gitignore - Copy the result and save it as
.gitignorein your project root
Common .gitignore Templates
Node.js
Node projects generate a node_modules/ directory containing all dependencies. This directory is typically the largest in the project and should always be ignored since npm install recreates it from package.json.
Python
Python creates __pycache__/ directories, .pyc bytecode files, virtual environments (venv/, .env/), and distribution artifacts (dist/, *.egg-info/).
Java
Java projects produce compiled .class files, JAR archives, and build output directories (target/ for Maven, build/ for Gradle).
Go
Go modules cache dependencies and produce binary outputs. The vendor/ directory may or may not be committed depending on team preference.
Best Practices
- Start with a template: Use this tool to get a solid baseline, then customize
- Be specific: Avoid overly broad patterns like
*that might catch important files - Document unusual rules: Add comments explaining non-obvious ignore patterns
- Use a global gitignore: Put OS and IDE patterns in
~/.gitignore_globalinstead of every project - Review regularly: As your project evolves, update the
.gitignoreto match - Never ignore the .gitignore: The
.gitignorefile itself should be committed
.gitignore vs .git/info/exclude
While .gitignore is committed and shared with all collaborators, .git/info/exclude works the same way but is local to your machine. Use exclude for personal patterns you don’t want to share (e.g., your specific editor’s files).
Fixing Already-Tracked Files
If you add a rule to .gitignore but the file is already tracked, Git continues tracking it. To fix this:
# Remove file from tracking (keeps the local file)
git rm --cached filename
# Remove entire directory from tracking
git rm -r --cached directory/
# Rebuild the index from scratch
git rm -r --cached .
git add .
git commit -m "Apply .gitignore rules"