This comprehensive guide covers all gitignore commands, options, and usage patterns for effective .gitignore file management.
# Initialize project with templates
gitignore init python vscode linux
# Add custom patterns
gitignore .env secrets/ *.log
# Auto-detect project type
gitignore auto
# Preview changes first
gitignore --dry-run init rust
| Scenario | Command | Description |
|---|---|---|
| New Python Project | gitignore init python vscode |
Language + editor templates |
| Node.js Application | gitignore init node linux docker |
Runtime + OS + container |
| Full-Stack App | gitignore init python node postgres |
Multiple technologies |
| Custom Patterns | gitignore .env dist/ build/ |
Project-specific ignores |
gitignore init [templates...]Initialize or update .gitignore with built-in templates.
Syntax:
gitignore init <template> [template...] [options]
Examples:
# Single template
gitignore init python
# Multiple templates
gitignore init python vscode linux
# With options
gitignore init rust --verbose --dry-run
Available Templates:
| Category | Templates | Description |
|---|---|---|
| Languages | python, node, rust, go, java, cpp, c |
Programming languages |
| Frameworks | django, flask, react, vue, angular |
Web frameworks |
| Tools | docker, kubernetes, terraform |
DevOps tools |
| Editors | vscode, intellij, vim, emacs |
IDE configurations |
| OS | linux, macos, windows |
Operating system files |
Features:
.gitignore before changesgitignore sync [templates...]Download and apply official templates from GitHubβs gitignore repository.
Syntax:
gitignore sync <template> [template...] [options]
Examples:
# Sync single template
gitignore sync python
# Sync multiple templates
gitignore sync rust go java
# Force refresh cache
gitignore sync python --no-cache
Network Features:
gitignore autoAutomatically detect project type and apply appropriate templates.
Detection Rules:
| File Pattern | Detected Template | Priority |
|---|---|---|
package.json |
node |
High |
requirements.txt, setup.py |
python |
High |
Cargo.toml |
rust |
High |
go.mod |
go |
High |
pom.xml, build.gradle |
java |
High |
Dockerfile |
docker |
Medium |
.vscode/ |
vscode |
Low |
Examples:
# Auto-detect and apply
gitignore auto
# Preview what would be applied
gitignore auto --dry-run
# Force re-detection
gitignore auto --force
gitignore [patterns...]Add custom patterns directly to .gitignore.
Syntax:
gitignore <pattern> [pattern...] [options]
Examples:
# Single pattern
gitignore node_modules/
# Multiple patterns
gitignore *.log .env dist/
# With wildcards
gitignore *.tmp *.swp *~
# Force add command names
gitignore --add init sync list
Pattern Intelligence:
/*, ?, [] patternsgitignore list [filter]List available templates with descriptions.
Syntax:
gitignore list [filter] [options]
Examples:
# List all templates
gitignore list
# Filter by language
gitignore list python
# Show detailed information
gitignore list --verbose
# List only built-in templates
gitignore list --builtin
Output Format:
Available Templates:
βββ Languages
β βββ python - Python projects
β βββ node - Node.js applications
β βββ rust - Rust projects
βββ Tools
β βββ docker - Docker containers
β βββ kubernetes - Kubernetes manifests
βββ Editors
βββ vscode - Visual Studio Code
βββ intellij - IntelliJ IDEA
gitignore show <template>Display template contents without applying.
Syntax:
gitignore show <template> [options]
Examples:
# Show template contents
gitignore show python
# Show with line numbers
gitignore show python --numbered
# Show multiple templates
gitignore show python node
gitignore interactiveLaunch interactive template selection interface.
Features:
Usage:
gitignore interactive
# or
gitignore -t
gitignore global initCreate and initialize global .gitignore file.
Setup Process:
# Initialize global gitignore
gitignore global init
# Configure Git to use it
git config --global core.excludesfile ~/.gitignore_global
gitignore global add [templates...]Add templates to global .gitignore.
Examples:
# Add OS-specific patterns
gitignore global add macos linux
# Add editor configurations
gitignore global add vscode intellij
gitignore backupCreate timestamped backup of current .gitignore.
Backup Location: .gitignore.backup.YYYY-MM-DD_HH-MM-SS
gitignore backupsList all available backups.
Output:
Available Backups:
βββ .gitignore.backup.2024-01-15_14-30-22 (2.1KB)
βββ .gitignore.backup.2024-01-10_09-15-33 (1.8KB)
βββ .gitignore.backup.2024-01-05_16-45-12 (1.5KB)
gitignore restore [backup]Restore .gitignore from backup.
Examples:
# Restore latest backup
gitignore restore
# Restore specific backup
gitignore restore .gitignore.backup.2024-01-10_09-15-33
# List backups first
gitignore backups
gitignore cache clearClear downloaded template cache.
Effects:
| Flag | Short | Description | Example |
|---|---|---|---|
--verbose |
-v |
Detailed output | gitignore -v init python |
--quiet |
-q |
Minimal output | gitignore -q init python |
--dry-run |
-n |
Preview changes | gitignore -n init python |
| Flag | Description | Example |
|---|---|---|
--force |
Overwrite without backup | gitignore --force init python |
--no-cache |
Skip cache for sync | gitignore sync python --no-cache |
--add |
Force pattern addition | gitignore --add init sync |
| Flag | Short | Description |
|---|---|---|
--help |
-h |
Show help information |
--version |
-V |
Show version information |
Combine multiple operations efficiently:
# Initialize, add patterns, backup
gitignore init python vscode && gitignore .env dist/ && gitignore backup
Web Development Stack:
gitignore init node react typescript vscode docker
Data Science Project:
gitignore init python jupyter vscode linux
System Administration:
gitignore init shell docker ansible linux
Create reusable setup scripts:
#!/bin/bash
# setup-gitignore.sh
# Initialize project
gitignore init python django vscode
# Add custom patterns
gitignore .env media/ staticfiles/
# Create backup
gitignore backup
echo "Gitignore setup complete!"
Automate gitignore management:
# .git/hooks/post-commit
#!/bin/bash
# Auto-backup after commits
gitignore backup > /dev/null
Gitignore uses sophisticated heuristics to distinguish commands from patterns:
Always Treated as Patterns:
node_modules/, src/main/*.log, temp*, file?.txt.env, .DS_Storemyfile.txt (if file exists)Command Conflicts (Require --add):
init, sync, list, show, autobackup, restore, globalError Handling:
$ gitignore sync
β Error: Ambiguous argument 'sync'
β Did you mean the 'sync' command? Use: gitignore sync <template>
β Or add as pattern? Use: gitignore --add sync
| Operation | Cache Type | Duration | Purpose |
|---|---|---|---|
| Template Downloads | File cache | 24 hours | Reduce network requests |
| Template Metadata | Memory | Session | Fast lookups |
| Configuration | Memory | Session | Avoid file I/O |
| Component | Typical Usage | Notes |
|---|---|---|
| Binary + Templates | ~2MB | Embedded templates |
| Runtime Memory | < 1MB | Configuration + buffers |
| Cache Storage | Variable | Downloaded templates |
YYYY-MM-DD_HH-MM-SSgitignore restore to recoverPreview all changes before applying:
$ gitignore --dry-run init python
[DRY RUN] Would create .gitignore with:
- Python.gitignore (148 patterns)
- Would backup existing .gitignore to .gitignore.backup.2024-01-15_14-30-22
- No actual changes made
# 1. Initialize repository
git init
# 2. Auto-detect project type
gitignore auto
# 3. Add additional templates
gitignore init vscode linux
# 4. Add custom patterns
gitignore .env secrets/ logs/
# 5. Create backup
gitignore backup
| Project Type | Recommended Templates | Rationale |
|---|---|---|
| Web App | node, react, vscode, linux |
Runtime + framework + editor + OS |
| API Service | python, docker, linux |
Language + container + OS |
| CLI Tool | rust, linux, macos |
Language + cross-platform |
| Data Science | python, jupyter, vscode |
Language + notebook + editor |
.gitignore changessync for latest official templates--dry-run for complex operationsSymptoms:
β Error: Template 'xyz' not found
Solutions:
# Check available templates
gitignore list
# Search for similar templates
gitignore list | grep -i xyz
# Use sync for official templates
gitignore sync xyz
Symptoms:
β Error: Permission denied writing .gitignore
Solutions:
# Check file permissions
ls -la .gitignore
# Fix permissions
chmod 644 .gitignore
# Check directory permissions
ls -ld .
Symptoms:
β Error: Network request failed
Solutions:
# Check internet connection
curl -I https://github.com
# Use cached templates
gitignore sync python # Will use cache if available
# Force offline mode
gitignore init python # Uses built-in templates
Symptoms:
β Error: Ambiguous argument 'init'
Solutions:
# For command usage
gitignore init python
# For pattern addition
gitignore --add init
# Get help
gitignore --help
# Show version and build info
gitignore --version
# Verbose output for debugging
gitignore --verbose init python
# Dry run to preview
gitignore --dry-run init python
# Check cache status
gitignore cache info # (if implemented)
# General help
gitignore --help
# Command-specific help
gitignore init --help
# List all templates
gitignore list
# Show template contents
gitignore show python