Skip to content

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.

  1. Command-line patterns (- for commands that support them)
  2. Directory .gitignore (patterns in same directory as file, up to repository root)
  3. $GIT_DIR/info/exclude (repository-specific exclusions)
  4. core.excludesFile (global exclusions from config file)
  • $XDG_CONFIG_HOME/git/ignore (global ignore file location)
PatternDescriptionExampleMatches
*Zero or more characters*.logdebug.log, error.log
?Single character?.tmpa.tmp, X.tmp
[abc]Any character in bracketstest[ABC].txttestA.txt, testC.txt
!Negation pattern!important.logKeep important.log
/Directory separatorbuild/build directory
**/Multi-level wildcardvendor/**/cache/Any cache dir in vendor
/**After directorysrc/**/*.jsAll JS files under src
# Matches any file named test.txt
test.txt
# Matches any directory named build
build/
# Matches any .log files in any logger directory
logger/*.log
# Root-level patterns (relative to .gitignore location)
*.log
!important.log
# Recursive patterns
**/.DS_Store
**/node_modules/
Terminal window
# Create project-specific gitignore
cat > .gitignore << 'EOF'
# Logs
*.log
logs/
# Build artifacts
dist/
build/
*.exe
# Temporary files
*.tmp
*.swp
# IDE files
.vscode/
.idea/
# Environment variables
.env
.env.local
EOF
Terminal window
# Configure global ignore file
git config --global core.excludesFile ~/.gitignore
# Create global gitignore
cat > ~/.gitignore << 'EOF'
# OS-specific files
.DS_Store
Thumbs.db
# Editor files
*.swp
*~
# IDE files (if not globally set)
# .vscode/ and .idea/ handled project-wise
EOF
Terminal window
# Create repository-specific excludes
cat > .git/info/exclude << 'EOF'
# Personal working files
my-workspace.layout
debug-notes.txt
EOF
Terminal window
# Check which files are ignored
git status --ignored
# Check if file is ignored
git check-ignore -v path/to/file
# List all ignored files
git ls-files --others --ignored --exclude-standard
Terminal window
# Add interactively, respecting ignores
echo "secret.txt" >> .gitignore
git add secret.txt # Won't add if in .gitignore
# Force add ignored file
git add --force secret.txt
Terminal window
# Use gitignore.io templates
# Visit https://www.tigon.io/gitignore and download template
# Example: Node.js template
cat >> .gitignore << 'EOF'
# Dependencies
node_modules/
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Coverage directory used by tools like istanbul
coverage/
*.lcov
EOF
Terminal window
# Check which pattern affects file
git check-ignore -v config/database.yml
# See ignored files in directory
git status --porcelain | grep "^!!"
# Force show all ignored
git status --ignored

Sample Language-Specific .gitignore Patterns:

Section titled “Sample Language-Specific .gitignore Patterns:”
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Coverage directory used by tools like istanbul
coverage/
*.lcov
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Dependency directories
jspm_packages/
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
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:
*.log
db.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.xml
hs_err_pid*
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# Gradle
.gradle/
gradle-app.setting
!gradle-wrapper.jar

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.

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.

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.

Yes! .gitignore ensures consistent ignores across team. Include language/OS specific patterns and project-specific exclusions.

No, .gitignore only contains ignore patterns, not file listings. Each pattern matches dynamically.

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.

No direct size-based ignoring. Use patterns by extension/type or post-commit hooks for policy enforcement.

  1. Build Artifact Exclusion: Automatically ignore generated binaries, caches, and temporary files
  2. Security Protection: Prevent accidental commit of passwords, secret keys, or sensitive data
  3. Development Environment Normalization: Standardize excluded IDE files, logs, and platform-specific artifacts
  4. Package Management: Exclude dependency directories (node_modules, vendor) from version control
  5. Collaboration Standards: Establish consistent file exclusions across development team
  6. Performance Optimization: Exclude unnecessary files to reduce repository size and clone times