git-ignore
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.
Features
- 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
Installation
From crates.io (recommended)
With cargo (fastest and most reliable):
Pre-built binaries
Download pre-built binaries for your platform from the releases page.
From source
Prerequisites: Rust 1.70+ (install via rustup)
The binary will be available at target/release/git-ignore.
For development
Usage
Basic Usage
Add patterns to the repository's .gitignore file:
Target Specific Files
Add patterns to .git/info/exclude (not shared with others):
Add patterns to your global gitignore:
Options
--local,-l: Add patterns to.git/info/excludeinstead 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
Examples
# Add Python-specific patterns
# Add build artifacts (local only, not shared)
# Add OS-specific patterns globally
# Allow duplicates (useful for scripting)
# Skip validation for special patterns
Pattern Validation
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)
)
Use --no-validate to skip validation when needed.
Exit Codes
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
Configuration
Global Gitignore Setup
To use --global, configure your global gitignore file:
# Set global gitignore file
# Or use the default location
Integration with Scripts
git-ignore is designed to work well in scripts:
#!/bin/bash
# Add standard Python patterns
if ; then
else
fi
Development
Setup
Running Tests
# Unit tests
# Integration tests
# All tests
Code Quality
# Linting
# Formatting
# Full check
Building
# Build release binary
# Test the built binary
Architecture
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.
Supported Platforms
- Rust: 1.70.0+
- Operating Systems: Linux, macOS, Windows
- Git: Any version with
git rev-parsesupport
Comparison with Alternatives
| 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 |
Troubleshooting
Common Issues
"Not in a git repository"
- Ensure you're running the command from within a git repository
- For
--localoption, the repository must have a.gitdirectory
"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
Getting Help
- Check this README and the
--helpoutput - Look at the integration tests for usage examples
- Open an issue on GitHub
Contributing
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.
License
MIT License. See LICENSE file for details.