Skip to content

cpinto/git-ignore

Repository files navigation

Git Ignore Extension

A comprehensive git extension for managing .gitignore files with intelligent autocomplete support for bash and zsh shells.

Features

  • Intuitive Command Interface: Manage .gitignore patterns using familiar git-style commands
  • Intelligent Autocomplete: Full tab completion for files, directories, and patterns in both bash and zsh
  • Smart Pattern Management: Add, remove, and list patterns with duplicate detection
  • File Status Checking: Verify whether files are ignored by current patterns
  • Repository Awareness: Works seamlessly within git repositories with proper root detection
  • Tracked File Warnings: Alerts when attempting to ignore already-tracked files

Installation

Quick Install

Clone the repository and run the installation script:

git clone <repository-url> git-ignore-extension
cd git-ignore-extension
chmod +x install.sh
./install.sh

Manual Installation

  1. Install the main script:

    cp git-ignore ~/.local/bin/
    chmod +x ~/.local/bin/git-ignore
  2. Add to PATH (if not already included):

    echo 'export PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc
  3. Install bash completion:

    mkdir -p ~/.local/share/bash-completion/completions
    cp git-ignore-completion.bash ~/.local/share/bash-completion/completions/git-ignore
  4. Install zsh completion:

    mkdir -p ~/.zsh/completions
    cp git-ignore-completion.zsh ~/.zsh/completions/_git-ignore

Shell Configuration

Bash Configuration

Add to your ~/.bashrc:

# Enable git-ignore completion
for file in ~/.local/share/bash-completion/completions/*; do
    [ -f "$file" ] && source "$file"
done

Zsh Configuration

Add to your ~/.zshrc:

# Add completion directory to fpath
fpath=(~/.zsh/completions $fpath)

# Initialise completion system
autoload -Uz compinit && compinit

Usage

Basic Commands

The extension supports both standalone and git subcommand usage:

git ignore <pattern>              # Add pattern (default action)
git ignore add <pattern>...       # Explicitly add patterns
git ignore remove <pattern>...    # Remove patterns
git ignore list                   # List all patterns
git ignore check <file>...        # Check if files are ignored
git ignore help                   # Show help information

Command Details

Adding Patterns

Add single or multiple patterns to .gitignore:

# Default add (without 'add' command)
git ignore "*.log"

# Explicit add command
git ignore add "*.tmp" "*.cache"

# Add directories
git ignore add build/ dist/ node_modules/

# Add with wildcards (use quotes)
git ignore add "*.py[co]" "__pycache__/"

Removing Patterns

Remove existing patterns from .gitignore:

git ignore remove "*.log"
git ignore rm "*.tmp"    # 'rm' is an alias for 'remove'

Listing Patterns

Display all patterns in the current .gitignore:

git ignore list
git ignore ls    # 'ls' is an alias for 'list'

Checking Files

Verify whether files match ignore patterns:

git ignore check file.txt
git ignore check src/main.py tests/*.py

Autocomplete Features

The extension provides context-aware autocomplete:

File and Directory Completion

When adding patterns, autocomplete suggests:

  • Untracked files in the repository
  • Directories (up to 2 levels deep)
  • Common ignore patterns (when no input is provided)
git ignore add src/<TAB>         # Completes files in src/
git ignore build/<TAB>            # Completes subdirectories
git ignore <TAB>                  # Shows files, directories, and commands

Pattern Removal Completion

When removing patterns, autocomplete shows existing patterns from .gitignore:

git ignore remove <TAB>          # Lists current .gitignore patterns
git ignore rm *.lo<TAB>          # Completes to *.log if it exists

Command Completion

git ignore <TAB>                 # Shows available commands
git ignore a<TAB>                # Completes to 'add'
git ignore r<TAB>                # Shows 'remove' and 'rm'

Common Patterns

The autocomplete system suggests common patterns when appropriate:

  • *.log - Log files
  • *.tmp - Temporary files
  • *.swp - Vim swap files
  • .DS_Store - macOS system files
  • node_modules/ - Node.js dependencies
  • build/ - Build directories
  • dist/ - Distribution directories
  • *.pyc - Python compiled files
  • __pycache__/ - Python cache directories
  • .env - Environment variable files
  • .vscode/ - VS Code settings
  • .idea/ - IntelliJ IDEA settings

Advanced Features

Pattern Normalisation

The extension automatically normalises patterns:

  • Removes leading ./ from paths
  • Handles relative paths correctly
  • Preserves pattern syntax (wildcards, negations)

Duplicate Detection

Prevents adding duplicate patterns and provides feedback:

$ git ignore add "*.log"
✓ Added: *.log
$ git ignore add "*.log"
⚠ Pattern already exists: *.log

Tracked File Detection

Warns when attempting to ignore files already tracked by git:

$ git ignore add src/main.py
✓ Added: src/main.py
⚠ File 'src/main.py' is already tracked by git. You may want to run:
  git rm --cached src/main.py

Repository Root Detection

Automatically operates from the repository root, ensuring consistent behaviour regardless of current working directory.

Troubleshooting

Command Not Found

If git ignore is not recognised:

  1. Ensure ~/.local/bin is in your PATH:

    echo $PATH | grep -q "$HOME/.local/bin" || echo "Not in PATH"
  2. Reload your shell configuration:

    source ~/.bashrc  # For bash
    source ~/.zshrc   # For zsh

Autocomplete Not Working

Bash

  1. Verify completion file exists:

    ls ~/.local/share/bash-completion/completions/git-ignore
  2. Check if completion is sourced in .bashrc

  3. Restart your terminal or run:

    source ~/.local/share/bash-completion/completions/git-ignore

Zsh

  1. Verify completion file exists:

    ls ~/.zsh/completions/_git-ignore
  2. Ensure fpath includes the completion directory:

    echo $fpath | grep -q ".zsh/completions" || echo "Not in fpath"
  3. Rebuild completion cache:

    rm -f ~/.zcompdump && compinit

Permission Issues

Ensure the script is executable:

chmod +x ~/.local/bin/git-ignore

Requirements

  • Git (version 2.0 or higher)
  • Bash 4.0+ or Zsh 5.0+
  • Standard Unix utilities (grep, sed, find)

File Structure

git-ignore-extension/
├── git-ignore                    # Main executable script
├── git-ignore-completion.bash    # Bash completion script
├── git-ignore-completion.zsh     # Zsh completion script
├── install.sh                    # Installation script
└── README.md                     # Documentation

Contributing

Contributions are welcome. When submitting changes:

  1. Maintain consistent code style
  2. Test with both bash and zsh shells
  3. Update documentation as needed
  4. Ensure autocomplete functionality remains intact

Licence

MIT

Author

Celso Pinto celso@celsopinto.com

About

A comprehensive git extension for managing .gitignore files with intelligent autocomplete support for bash and zsh shells.

Resources

License

Stars

Watchers

Forks

Contributors