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 / JavaScript
Node projects generate a node_modules/ directory containing all dependencies — often hundreds of megabytes. Always ignore it since npm install or yarn install recreates it from package.json. Also ignore .next/ for Next.js, .nuxt/ for Nuxt, dist/ for bundled output, and .env files containing API keys.
node_modules/
dist/
.next/
.nuxt/
.env
.env.local
*.tsbuildinfo
Python
Python creates __pycache__/ directories, .pyc bytecode files, virtual environments (venv/, .env/), and distribution artifacts (dist/, *.egg-info/). If you use Jupyter, ignore .ipynb_checkpoints/.
__pycache__/
*.py[cod]
venv/
.env
dist/
*.egg-info/
.ipynb_checkpoints/
Java
Java projects produce compiled .class files, JAR archives, and build output directories (target/ for Maven, build/ for Gradle). IDE-specific directories like .idea/ (IntelliJ) or .settings/ (Eclipse) should also be ignored.
Go
Go modules cache dependencies and produce binary outputs. The vendor/ directory may or may not be committed depending on team preference.
Rust
Rust stores compiled artifacts in the target/ directory, which can grow very large. The Cargo.lock file is typically committed for binaries but ignored for libraries.
target/
*.pdb
C# / .NET
.NET projects produce bin/ and obj/ directories, NuGet packages in packages/, and user-specific project settings (.suo, .user files).
bin/
obj/
packages/
*.suo
*.user
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"
Debugging .gitignore Issues
When your .gitignore rules seem to have no effect, work through these checks:
- Already tracked? The most common cause. Run
git ls-files <path>— if the file appears, it’s tracked and you needgit rm --cachedfirst. - Wrong directory? A
.gitignoreinfrontend/won’t affect files inbackend/. Place the rule in the correct.gitignoreor in the root one. - Pattern syntax error? A leading slash (
/build) anchors the pattern to that.gitignore’s directory. Without the slash,buildmatches at any depth. - Negation override? A later
!rule can re-include files you thought were ignored. Rungit check-ignore -v <file>to see which rule is matching (or not matching) a specific path. - Whitespace? Trailing spaces in a pattern can cause silent mismatches. Check with
cat -A .gitignore.
# Show which .gitignore rule applies to a file
git check-ignore -v path/to/file
# List all ignored files
git status --ignored
Setting Up a Global Gitignore
OS-specific files (.DS_Store, Thumbs.db) and editor directories (.idea/, .vscode/, *.swp) clutter every repository if you add them per-project. A global .gitignore solves this once:
# Tell Git where your global ignore file lives
git config --global core.excludesfile ~/.gitignore_global
Then add your patterns to ~/.gitignore_global:
# macOS
.DS_Store
._*
# Windows
Thumbs.db
Desktop.ini
# Editors
.idea/
.vscode/
*.swp
*~
This keeps project .gitignore files focused on project-specific rules while your personal environment noise is handled globally.
.gitignore Generator vs GitHub Templates
GitHub offers a collection of .gitignore templates at github/gitignore. These are single-language templates — one file per language. Our generator lets you combine multiple languages, frameworks, IDEs, and operating systems into a single file, which is closer to what real projects need. A typical web project, for example, needs Node.js rules, IDE ignores, and OS file patterns all in one .gitignore.