ignore Git Command Guide
The .gitignore file specifies intentionally untracked files that Git should ignore. It uses patterns to match file paths, allowing developers to exclude generated files, temporary files, and sensitive data from version control.
Git Ignore Pattern Sources:
Section titled “Git Ignore Pattern Sources:”Pattern Precedence (Highest to Lowest):
Section titled “Pattern Precedence (Highest to Lowest):”- Command-line patterns (- for commands that support them)
- Directory .gitignore (patterns in same directory as file, up to repository root)
- $GIT_DIR/info/exclude (repository-specific exclusions)
- core.excludesFile (global exclusions from config file)
Special Sources:
Section titled “Special Sources:”$XDG_CONFIG_HOME/git/ignore(global ignore file location)
Pattern Syntax and Matching:
Section titled “Pattern Syntax and Matching:”Basic Pattern Types:
Section titled “Basic Pattern Types:”| Pattern | Description | Example | Matches |
|---|---|---|---|
* | Zero or more characters | *.log | debug.log, error.log |
? | Single character | ?.tmp | a.tmp, X.tmp |
[abc] | Any character in brackets | test[ABC].txt | testA.txt, testC.txt |
! | Negation pattern | !important.log | Keep important.log |
/ | Directory separator | build/ | build directory |
**/ | Multi-level wildcard | vendor/**/cache/ | Any cache dir in vendor |
/** | After directory | src/**/*.js | All JS files under src |
Pattern Matching Rules:
Section titled “Pattern Matching Rules:”File vs Directory Patterns:
Section titled “File vs Directory Patterns:”# Matches any file named test.txttest.txt
# Matches any directory named buildbuild/
# Matches any .log files in any logger directorylogger/*.logLocation-Based Patterns:
Section titled “Location-Based Patterns:”# Root-level patterns (relative to .gitignore location)*.log!important.log
# Recursive patterns**/.DS_Store**/node_modules/Creating and Managing GitIgnore Files:
Section titled “Creating and Managing GitIgnore Files:”Project .gitignore (Repository Root):
Section titled “Project .gitignore (Repository Root):”# Create project-specific gitignorecat > .gitignore << 'EOF'# Logs*.loglogs/
# Build artifactsdist/build/*.exe
# Temporary files*.tmp*.swp
# IDE files.vscode/.idea/
# Environment variables.env.env.localEOFGlobal .gitignore (All Repositories):
Section titled “Global .gitignore (All Repositories):”# Configure global ignore filegit config --global core.excludesFile ~/.gitignore
# Create global gitignorecat > ~/.gitignore << 'EOF'# OS-specific files.DS_StoreThumbs.db
# Editor files*.swp*~
# IDE files (if not globally set)# .vscode/ and .idea/ handled project-wiseEOFRepository-Specific Exclusions:
Section titled “Repository-Specific Exclusions:”# Create repository-specific excludescat > .git/info/exclude << 'EOF'# Personal working filesmy-workspace.layoutdebug-notes.txtEOFCheck and Verify Patterns:
Section titled “Check and Verify Patterns:”# Check which files are ignoredgit status --ignored
# Check if file is ignoredgit check-ignore -v path/to/file
# List all ignored filesgit ls-files --others --ignored --exclude-standardInteractive Add with Ignores:
Section titled “Interactive Add with Ignores:”# Add interactively, respecting ignoresecho "secret.txt" >> .gitignoregit add secret.txt # Won't add if in .gitignore
# Force add ignored filegit add --force secret.txtTemplate-Based Setup:
Section titled “Template-Based Setup:”# Use gitignore.io templates# Visit https://www.tigon.io/gitignore and download template
# Example: Node.js templatecat >> .gitignore << 'EOF'# Dependenciesnode_modules/npm-debug.log*
# Runtime datapids*.pid*.seed*.pid.lock
# Coverage directory used by tools like istanbulcoverage/*.lcovEOFTroubleshoot Ignore Issues:
Section titled “Troubleshoot Ignore Issues:”# Check which pattern affects filegit check-ignore -v config/database.yml
# See ignored files in directorygit status --porcelain | grep "^!!"
# Force show all ignoredgit status --ignoredSample Language-Specific .gitignore Patterns:
Section titled “Sample Language-Specific .gitignore Patterns:”Node.js/JavaScript:
Section titled “Node.js/JavaScript:”# Dependenciesnode_modules/npm-debug.log*yarn-debug.log*yarn-error.log*
# Runtime datapids*.pid*.seed*.pid.lock
# Coverage directory used by tools like istanbulcoverage/*.lcov
# Logslogs*.lognpm-debug.log*yarn-debug.log*yarn-error.log*
# Dependency directoriesjspm_packages/
# Output of 'npm pack'*.tgz
# Yarn Integrity file.yarn-integrityPython:
Section titled “Python:”# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/*.egg-info/.installed.cfg*.egg
# PyInstaller*.manifest*.spec
# Virtual environments.venv/venv/ENV/
# Django stuff:*.logdb.sqlite3# Compiled class file*.class
# Log file*.log
# BlueJ files*.ctxt
# Mobile Tools for Java (J2ME).mtj.tmp/
# Package Files #*.jar*.war*.nar*.ear*.zip*.tar.gz*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xmlhs_err_pid*
# Maventarget/pom.xml.tagpom.xml.releaseBackuppom.xml.versionsBackuppom.xml.nextrelease.propertiesdependency-reduced-pom.xmlbuildNumber.properties.mvn/timing.properties
# Gradle.gradle/gradle-app.setting!gradle-wrapper.jarHow do .gitignore patterns work?
Section titled “How do .gitignore patterns work?”Patterns use glob syntax to match file paths. * matches anything, ? single character, [abc] character sets. Leading / makes patterns root-relative. Leading ! negates patterns.
What is the difference between .gitignore and .git/info/exclude?
Section titled “What is the difference between .gitignore and .git/info/exclude?”.gitignore is version-controlled and shared. .git/info/exclude is local to repository and not shared. Use .gitignore for project standards, exclude for personal preferences.
Why isn’t my .gitignore working?
Section titled “Why isn’t my .gitignore working?”Check if file is already tracked (git rm —cached). Check for syntax errors. Verify path relativity. Use git check-ignore -v filename to debug which pattern applies.
Can I ignore tracked files?
Section titled “Can I ignore tracked files?”No, ignores only affect untracked files. To stop tracking tracked files, use git rm —cached. Then .gitignore will prevent re-addition.
How do global gitignore and local gitignore interact?
Section titled “How do global gitignore and local gitignore interact?”Both apply, local patterns override global. Global affects all repositories, local overrides for specific project needs.
Should I track .gitignore?
Section titled “Should I track .gitignore?”Yes! .gitignore ensures consistent ignores across team. Include language/OS specific patterns and project-specific exclusions.
Can I include other files in .gitignore?
Section titled “Can I include other files in .gitignore?”No, .gitignore only contains ignore patterns, not file listings. Each pattern matches dynamically.
How do I ignore files in subdirectories?
Section titled “How do I ignore files in subdirectories?”Use relative paths or **/ for multi-level matching. Patterns are relative to .gitignore location.
What’s the difference between .gitignore and .gitkeep?
Section titled “What’s the difference between .gitignore and .gitkeep?”Different concerns: .gitignore excludes files, .gitkeep allows tracking empty directories by creating minimal file.
Can I use Git LFS to ignore and track large files?
Section titled “Can I use Git LFS to ignore and track large files?”Different approaches: .gitignore excludes files, Git LFS tracks large files with pointer replacement.
How do I ignore files by file size?
Section titled “How do I ignore files by file size?”No direct size-based ignoring. Use patterns by extension/type or post-commit hooks for policy enforcement.
Applications of the .gitignore patterns
Section titled “Applications of the .gitignore patterns”- Build Artifact Exclusion: Automatically ignore generated binaries, caches, and temporary files
- Security Protection: Prevent accidental commit of passwords, secret keys, or sensitive data
- Development Environment Normalization: Standardize excluded IDE files, logs, and platform-specific artifacts
- Package Management: Exclude dependency directories (node_modules, vendor) from version control
- Collaboration Standards: Establish consistent file exclusions across development team
- Performance Optimization: Exclude unnecessary files to reduce repository size and clone times