A command-line tool for easily adding patterns to git ignore files. Supports adding patterns to .gitignore
, .git/info/exclude
, or global gitignore files with validation and duplicate detection.
- Multiple target files: Add patterns to
.gitignore
,.git/info/exclude
, or global gitignore - Smart duplicate detection: Automatically avoids adding duplicate patterns
- Pattern validation: Warns about potentially problematic patterns
- Git repository awareness: Works with regular repos, submodules, and worktrees
- Cross-platform: Works on Linux, macOS, and Windows
- Fast and reliable: Minimal dependencies, comprehensive test coverage
With cargo (fastest and most reliable):
cargo install git-ignore-tool
Download pre-built binaries for your platform from the releases page.
Prerequisites: Rust 1.70+ (install via rustup)
git clone https://github.com/andrewleech/git-ignore.git
cd git-ignore
cargo build --release
The binary will be available at target/release/git-ignore
.
git clone https://github.com/andrewleech/git-ignore.git
cd git-ignore
cargo build
cargo test
Add patterns to the repository's .gitignore
file:
git ignore "*.pyc" "__pycache__/" "build/"
Add patterns to .git/info/exclude
(not shared with others):
git ignore --local "*.tmp" "my-personal-notes.txt"
Add patterns to your global gitignore:
git ignore --global "*.log" ".DS_Store"
--local
,-l
: Add patterns to.git/info/exclude
instead of.gitignore
--global
,-g
: Add patterns to global gitignore file--no-validate
: Skip pattern validation--allow-duplicates
: Allow duplicate patterns to be added--version
,-v
: Show version information--help
,-h
: Show help message
# Add Python-specific patterns
git ignore "*.pyc" "*.pyo" "__pycache__/" ".pytest_cache/"
# Add build artifacts (local only, not shared)
git ignore --local "build/" "dist/" "*.egg-info/"
# Add OS-specific patterns globally
git ignore --global ".DS_Store" "Thumbs.db" "*.swp"
# Allow duplicates (useful for scripting)
git ignore --allow-duplicates "*.log"
# Skip validation for special patterns
git ignore --no-validate "*"
git ignore automatically validates patterns and provides feedback:
- ERROR: Patterns that would corrupt the ignore file (e.g., containing newlines)
- WARNING: Potentially problematic patterns (e.g., very broad patterns like
*
) - INFO: Patterns that could be simplified (e.g.,
./file
→file
)
$ git ignore "*"
WARNING: Potentially problematic patterns:
*: Pattern is very broad and may ignore more than intended
Added 1 pattern to repository gitignore (.gitignore):
*
Use --no-validate
to skip validation when needed.
git-ignore uses semantic exit codes:
0
: Success1
: Pattern validation failed2
: Git repository issues (not in git repo, etc.)3
: Configuration issues (no global gitignore configured, etc.)4
: File system issues (permission denied, disk full, etc.)130
: Interrupted by user (Ctrl+C)255
: Unexpected error
To use --global
, configure your global gitignore file:
# Set global gitignore file
git config --global core.excludesfile ~/.gitignore_global
# Or use the default location
mkdir -p ~/.config/git
touch ~/.config/git/ignore
git ignore is designed to work well in scripts:
#!/bin/bash
# Add standard Python patterns
if git ignore "*.pyc" "__pycache__/" ".pytest_cache/"; then
echo "Added Python ignore patterns"
else
echo "Failed to add patterns (exit code: $?)" >&2
exit 1
fi
git clone https://github.com/andrewleech/git-ignore.git
cd git-ignore
cargo build
# Unit tests
cargo test --lib
# Integration tests
cargo test --test integration_tests
# All tests
cargo test
# Linting
cargo clippy -- -D warnings
# Formatting
cargo fmt --check
# Full check
cargo clippy --all-targets --all-features -- -D warnings
# Build release binary
cargo build --release
# Test the built binary
./target/release/git-ignore --version
The project consists of three main modules:
git.rs
: Git repository detection and path resolutionignore.rs
: Core ignore file operations (reading, writing, validation)main.rs
: CLI interface and argument parsing
See CLAUDE.md for detailed development information.
- Rust: 1.70.0+
- Operating Systems: Linux, macOS, Windows
- Git: Any version with
git rev-parse
support
Feature | git-ignore | Manual editing | Other tools |
---|---|---|---|
Duplicate detection | ✅ | ❌ | Varies |
Pattern validation | ✅ | ❌ | Limited |
Multiple targets | ✅ | Manual | Limited |
Git integration | ✅ | ❌ | Basic |
Cross-platform | ✅ | ✅ | Varies |
Batch operations | ✅ | Tedious | Limited |
"Not in a git repository"
- Ensure you're running the command from within a git repository
- For
--local
option, the repository must have a.git
directory
"No global gitignore file configured"
- Set up a global gitignore file:
git config --global core.excludesfile ~/.gitignore_global
- Or create the default location:
~/.config/git/ignore
Permission errors
- Ensure you have write permissions to the target file
- For global gitignore, ensure the directory exists and is writable
Patterns not working as expected
- Check pattern validation warnings
- Verify patterns follow gitignore syntax
- Check this README and the
--help
output - Look at the integration tests for usage examples
- Open an issue on GitHub
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass and code is formatted
- Submit a pull request
See CLAUDE.md for development guidelines.
MIT License. See LICENSE file for details.