gitignore

Usage Guide

This comprehensive guide covers all gitignore commands, options, and usage patterns for effective .gitignore file management.

πŸ“‹ Table of Contents

πŸš€ Quick Start

Basic Usage Patterns

# 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

Common Workflows

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

πŸ“– Command Reference

Core Commands

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 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 auto

Automatically 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:

Template Management

gitignore 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

Interactive Commands

gitignore interactive

Launch interactive template selection interface.

Features:

Usage:

gitignore interactive
# or
gitignore -t

Global Gitignore Management

gitignore global init

Create 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

Backup & Recovery

gitignore backup

Create timestamped backup of current .gitignore.

Backup Location: .gitignore.backup.YYYY-MM-DD_HH-MM-SS

gitignore backups

List 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

Cache Management

gitignore cache clear

Clear downloaded template cache.

Effects:

βš™οΈ Global Options

Output Control

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

Behavior Modification

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

Information

Flag Short Description
--help -h Show help information
--version -V Show version information

🎨 Advanced Usage

Command Chaining

Combine multiple operations efficiently:

# Initialize, add patterns, backup
gitignore init python vscode && gitignore .env dist/ && gitignore backup

Template Combinations

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

Custom Workflow Scripts

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!"

Integration with Git Hooks

Automate gitignore management:

# .git/hooks/post-commit
#!/bin/bash
# Auto-backup after commits
gitignore backup > /dev/null

🧠 Pattern Intelligence

Automatic Detection Logic

Gitignore uses sophisticated heuristics to distinguish commands from patterns:

Always Treated as Patterns:

Command Conflicts (Require --add):

Error 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

πŸ“Š Performance Optimization

Caching Strategies

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

Memory Usage

Component Typical Usage Notes
Binary + Templates ~2MB Embedded templates
Runtime Memory < 1MB Configuration + buffers
Cache Storage Variable Downloaded templates

πŸ›‘οΈ Safety Features

Automatic Backups

Dry Run Mode

Preview 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

Conflict Resolution

🎯 Best Practices

Project Setup Workflow

# 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

Template Selection Guidelines

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

Maintenance Practices

Performance Tips

πŸ†˜ Troubleshooting

Common Issues & Solutions

β€œTemplate not found” Error

Symptoms:

βœ— 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

Permission Denied

Symptoms:

βœ— Error: Permission denied writing .gitignore

Solutions:

# Check file permissions
ls -la .gitignore

# Fix permissions
chmod 644 .gitignore

# Check directory permissions
ls -ld .

Network Connection Issues

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

Command vs Pattern Confusion

Symptoms:

βœ— Error: Ambiguous argument 'init'

Solutions:

# For command usage
gitignore init python

# For pattern addition
gitignore --add init

# Get help
gitignore --help

Diagnostic Commands

# 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)

Getting Help

# General help
gitignore --help

# Command-specific help
gitignore init --help

# List all templates
gitignore list

# Show template contents
gitignore show python