G
GitIgnore.pro

The Complete .gitignore Guide

Master Git file exclusion with our comprehensive guide covering syntax, patterns, best practices, and solutions to common problems.

Beginner friendly
Complete examples
Best practices

What is .gitignore?

A .gitignore file is a text file that tells Git which files and directories to ignore in a project. When Git sees a .gitignore file, it uses the patterns inside to decide which files should not be tracked or committed to the repository.

Why do you need .gitignore?

  • Prevent sensitive data (passwords, API keys) from being committed
  • Exclude build artifacts and compiled files
  • Ignore dependencies that can be reinstalled
  • Keep OS and editor-specific files out of your repository
  • Maintain a clean, focused project history

Basic Syntax & Patterns

Fundamental Rules

1. Ignore Specific Files

# Ignore a specific file
secret.txt
config.env
database.db

2. Ignore File Types

# Ignore all files with specific extensions
*.log
*.tmp
*.cache
*.DS_Store

3. Ignore Directories

# Ignore entire directories
node_modules/
build/
dist/
.vscode/

Important Notes

  • File and directory names are case-sensitive
  • Trailing spaces are ignored unless quoted with backslash
  • Empty lines are ignored and can be used for readability
  • Lines starting with # are comments

Advanced Patterns

Negation (Exception Rules)

Use ! to create exceptions to ignore rules:

# Ignore all .txt files
*.txt

# But don't ignore important.txt
!important.txt

# Ignore all files in logs directory
logs/*

# But keep the directory structure
!logs/.gitkeep

Directory-Specific Patterns

# Only ignore file.txt in the root directory
/file.txt

# Ignore file.txt in any directory
file.txt

# Ignore all .log files in any subdirectory
**/*.log

Best Practices

✅ Do This

  • • Create .gitignore before first commit
  • • Use comments to organize sections
  • • Be specific rather than overly broad
  • • Include common IDE and OS files
  • • Test your patterns before committing

❌ Avoid This

  • • Don't ignore the .gitignore file itself
  • • Don't use overly complex patterns
  • • Don't ignore files that others need
  • • Don't commit sensitive data first
  • • Don't ignore too broadly

Common Problems & Solutions

Problem: Files Still Being Tracked

The most common issue: you added files to .gitignore but Git is still tracking them.

Why this happens

Git only ignores untracked files. If files were committed before being added to .gitignore, they remain tracked until explicitly removed.

Solution:

# Remove files from Git's tracking
git rm -r --cached .

# Re-add everything (now respecting .gitignore)
git add .

# Commit the changes
git commit -m "Apply .gitignore rules"

Problem: .gitignore Not Working

Check these common causes:

  • File location: Must be in repository root
  • File name: Must be exactly .gitignore
  • Syntax errors: Check for typos in patterns
  • File encoding: Must be UTF-8

Language-Specific Examples

Node.js / JavaScript

# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*

# Build outputs
dist/
build/
*.min.js

# Environment files
.env
.env.local

# IDE
.vscode/
.idea/

Python

# Byte-compiled files
__pycache__/
*.py[cod]
*$py.class

# Virtual environments
venv/
env/
.env

# IDE
.vscode/
.idea/

# Distribution
build/
dist/
*.egg-info/

💡 Pro Tip

Use our interactive generator to create custom .gitignore files for multiple technologies at once!

Global .gitignore

Set up a global .gitignore file for patterns that apply to all your projects (like OS files, editor configurations, etc.).

Setup Instructions

Step 1: Create global .gitignore

# Create the file in your home directory
touch ~/.gitignore_global

Step 2: Configure Git to use it

# Tell Git to use your global .gitignore
git config --global core.excludesfile ~/.gitignore_global

Recommended Global Patterns

# OS generated files
.DS_Store
Thumbs.db

# Editor files
*~
*.swp
*.swo
.vscode/
.idea/

# Temporary files
*.tmp
*.temp
*.cache

Troubleshooting

Check if a file is ignored

# Check if a specific file is ignored
git check-ignore -v filename.txt

# Check multiple files
git check-ignore *

Debug .gitignore patterns

# Show which .gitignore rule is ignoring a file
git check-ignore -v path/to/file

# List all ignored files
git ls-files --others --ignored --exclude-standard

When to force add ignored files

Only force add files when you're certain they should be tracked despite matching ignore patterns. Consider creating more specific patterns instead.

Ready to Create Your .gitignore?

Use our professional generator to create the perfect .gitignore file for your project in seconds.

Generate .gitignore File