AI-powered git commit message rewriter using Ollama or GPT
https://github.com/f/git-rewrite-commits.git
AI-powered git commit message rewriter using AI
Automatically rewrite your entire git commit history with better, conventional commit messages using AI. Perfect for cleaning up messy commit histories before open-sourcing projects or improving repository maintainability.
๐ Read the blog post: Fix Your Messy Commit History with AI
This tool rewrites git history, which is generally NOT recommended for shared repositories!
> When to use:
- Personal projects before making them public
- Feature branches before merging (with team agreement)
- Cleaning up local commits before pushing
- Preparing repositories for open-sourcing
> When NOT to use:
- On shared branches without team coordination
- After pushing commits that others have pulled
- On main/master branches of team projects
- In repositories where commit hashes are referenced
> Remember: Rewriting history changes commit hashes and requires force-pushing, which can disrupt your team's workflow.
๐ Important Privacy Notice: When using remote AI providers (OpenAI), this tool sends your file lists and diffs to external APIs.
Security Features:
# Use local Ollama instead of remote APIs
git config hooks.commitProvider ollama
git config hooks.providerModel gemma3
ollama pull gemma3
ollama serve
๐ See SECURITY.md for complete security documentation
--prompt for unique styles--max-commitsCOMMIT_MESSAGE.md filenpx git-rewrite-commits
# or shorter:
npx grec
Set OPENAI_API_KEY as environment variable. And:
git add -A
git commit -m "$(npx -y grec --quiet --staged --skip-remote-consent)"
> [main 9979e7e] feat(specs): update subscription status handling in webhook controller
3 files changed, 432 insertions(+), 9 deletions(-)
create mode 100644 spec/controllers/webhooks_spec.rb
git push origin master
# Full command name
npm install -g git-rewrite-commits
# Or install the short alias (grec = git-rewrite-commits)
npm install -g grec
# Both work identically:
git-rewrite-commits --help
grec --help # Same thing, just shorter!
โ Works on all platforms: Windows, macOS, Linux
๐ก Tip:grecis a shorter alias forgit-rewrite-commits- use whichever you prefer!
Step 1: Install or update the AI commit message hooks
npx git-rewrite-commits --install-hooks
# or with the short alias:
npx grec --install-hooks
๐ก Updates existing hooks: If hooks already exist, they'll be updated to the latest version. Non-git-rewrite-commits hooks are backed up before replacement.
Step 2: Enable the hooks you want (opt-in required for security):
# Option A: Enable message preview before commit
git config hooks.preCommitPreview true
# Option B: Enable automatic message generation
git config hooks.prepareCommitMsg true
# Or enable both for the full experience!
# For privacy: use local Ollama instead of remote OpenAI
git config hooks.commitProvider ollama
git config hooks.providerModel llama3.2 # Optional: specify model
Step 3: Configure your AI provider:
# Option A: OpenAI (sends data to remote API)
export OPENAI_API_KEY="your-api-key" # Unix/macOS
# set OPENAI_API_KEY="your-api-key" # Windows
# Option B: Ollama (local processing, recommended for sensitive repos)
ollama pull llama3.2
ollama serve
# Option C: Ollama on a custom server/port
export OLLAMA_URL="http://192.168.1.100:11434" # Unix/macOS
# set OLLAMA_URL=http://192.168.1.100:11434 # Windows
See QUICKSTART.md for detailed setup guide
# Set your template format
git config hooks.commitTemplate "[JIRA-XXX] feat: message"
# Set language
git config hooks.commitLanguage "es" # Spanish, French, etc.
# Set specific model
git config hooks.providerModel "gpt-4" # or "gpt-3.5-turbo", "llama3.2", etc.
# Set custom Ollama server URL (if not using localhost:11434)
git config hooks.ollamaUrl "http://192.168.1.100:11434"
COMMIT_MESSAGE.mdYou can provide project-specific commit message guidelines that the AI will follow by creating a COMMIT_MESSAGE.md file. The tool searches for this file in the following locations (in order):
./COMMIT_MESSAGE.md./.git/COMMIT_MESSAGE.md./.github/COMMIT_MESSAGE.mdCOMMIT_MESSAGE.md:
# Project Commit Guidelines
## Requirements
- Use conventional commits with these scopes: auth, api, ui, db
- Include ticket numbers when available (e.g., JIRA-123)
- Security changes must be clearly marked
- Breaking changes need BREAKING CHANGE in the message
## Project Context
This is a financial services API that handles sensitive data.
Emphasize security, compliance, and performance in commit messages.
See COMMIT_MESSAGE.md.example in this repository for a complete example.
# Using the full command name
npx git-rewrite-commits [options]
# Or using the short alias (grec)
npx grec [options]
Common use cases:
# Rewrite entire git history
npx git-rewrite-commits
# Preview changes without applying (dry run)
npx git-rewrite-commits --dry-run
# Generate commit message for staged changes
npx git-rewrite-commits --staged
# Process only last 10 commits
npx git-rewrite-commits --max-commits 10
# Use custom AI model
npx git-rewrite-commits --model gpt-4
# Use local AI with Ollama
npx git-rewrite-commits --provider ollama
# Install/update git hooks
npx git-rewrite-commits --install-hooks
Two Smart Hooks:
# Install the hooks (using either command)
npx git-rewrite-commits --install-hooks
# or
npx grec --install-hooks
# Enable them (opt-in for security)
git config hooks.preCommitPreview true # Preview before commit
git config hooks.prepareCommitMsg true # Auto-generate in editor
# Configure provider
git config hooks.commitProvider ollama # or use OpenAI with OPENAI_API_KEY
git config hooks.providerModel llama3.2 # Optional: specify model
Now when you run git commit:
# Clean up the last 5 commits before pushing
echo "๐ง Improving commit messages before push..."
npx git-rewrite-commits --max-commits 5 --dry-run
echo "Apply changes? (y/n)"
read answer
if [ "$answer" = "y" ]; then
Add to your ~/.gitconfig or ~/.zshrc/~/.bashrc:
# Git alias
git config --global alias.fix-commits '!npx git-rewrite-commits --max-commits'
# Usage: git fix-commits 3
# Shell alias
alias fix-last-commit='npx git-rewrite-commits --max-commits 1 --skip-backup'
alias fix-branch='npx git-rewrite-commits --max-commits 20'
# Usage: fix-last-commit
Before creating a pull request:
# 1. Check what needs fixing
npx git-rewrite-commits --dry-run --max-commits 10
# 2. Apply improvements
npx git-rewrite-commits --max-commits 10
# 3. Force push to your feature branch
git push --force-with-lease origin feature-branch
Add to your CI pipeline (e.g., GitHub Actions) for PR validation:
- name: Check Commit Quality
run: |
npx git-rewrite-commits --dry-run --max-commits ${{ github.event.pull_request.commits }}
# This will show which commits would be improved
Before making a private repo public:
# Fix all commits with custom template
npx git-rewrite-commits \
--template "feat(scope): message" \
--language en \
--no-skip-well-formed
# Review the changes
git log --oneline -20
# If satisfied, force push
git push --force-with-lease origin main
git commit -m "bad message", pre-commit can:
git filter-branch to apply new messagesContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
git checkout -b feature/AmazingFeature)git push origin feature/AmazingFeature)This project is licensed under the MIT License - see the LICENSE file for details.
If you discover any bugs, please create an issue here.
For questions and support, please open an issue in the GitHub repository.