git-rewrite-commits is a free AI-powered command-line tool that automatically rewrites git commit history with better, conventional commit messages.
It uses OpenAI GPT models or local AI models through Ollama to analyze code changes and generate meaningful commit descriptions that follow industry standards.
The tool addresses a problem familiar to most developers: messy commit histories filled with vague messages like “fix stuff”, “update”, or “wip”.
While these quick commits make sense during rapid development, they create problems when preparing code for pull requests, open-sourcing projects, or maintaining long-term repository readability.
git-rewrite-commits solves this by automatically analyzing each commit’s diff and changed files, then using AI to generate clear, conventional commit messages that accurately describe what changed and why.
Features
- Local AI Support: Runs completely offline using Ollama with models such as Llama, Mistral, or Codellama. No API costs and no data leaves your machine.
- One-Command Hook Installation: Install git hooks instantly with a single command that sets up automatic commit message generation for all future commits.
- Smart Detection System: Automatically evaluates commit message quality using a 10-point scoring system. Skips well-formed commits (scoring 7/10 or higher) to save time and API costs.
- Custom Templates: Define your own commit format patterns like “[JIRA-XXX] feat: message” or “type(scope): message” that the AI follows when generating messages.
- Custom Prompts: Override default AI instructions to create unique message styles, from emoji-rich commits to haiku-style messages to extremely technical descriptions.
- Multi-language Support: Generate commit messages in over 20 languages, including Spanish, French, Chinese, and Japanese.
- Conventional Commits Standard: Follows the widely adopted conventional commit format with types like feat, fix, docs, refactor, test, and chore.
- Safety Features: Creates backup branches automatically before rewriting history. Includes dry-run mode to preview changes without modifying the repository.
- Flexible Processing: Process your entire history, or limit to just the last N commits for faster operation on large repositories.
- Quality Threshold Customization: Adjust the minimum quality score (default 7/10) to control which commits get rewritten based on your standards.
- Progress Tracking: Real-time colored output shows exactly what the tool is doing as it processes commits.
Use Cases
- Preparing open source releases: Clean up personal project history before making repositories public by converting vague commits like “fixed stuff” to proper “fix(auth): resolve token validation edge case”
- Team feature branch cleanup: Improve commit messages on feature branches before creating pull requests, making code review more efficient with clear change explanations
- Legacy project maintenance: Rewrite historical commits in older projects where message quality was inconsistent, improving git blame usefulness
- Educational environments: Help developers learn conventional commit standards by seeing AI-generated examples that follow best practices
- International team standardization: Generate commit messages in multiple languages while maintaining consistent formatting across distributed teams
How To Use It
1. Set up your AI Provider
For OpenAI (cloud-based approach):
Export your API key as an environment variable. Get your key from the OpenAI platform at https://platform.openai.com/api-keys, then run:
export OPENAI_API_KEY="your-api-key-here"For Ollama (local approach):
Install Ollama from https://ollama.ai, then download a model and start the server:
ollama pull llama3.2
ollama serve2. Use git-rewrite-commits with npx:
cd your-repo
npx git-rewrite-commitsThis processes your entire commit history. For most use cases, you’ll want to limit the scope:
# Process only the last 10 commits
npx git-rewrite-commits --max-commits 10
# Preview changes without modifying anything
npx git-rewrite-commits --dry-run --max-commits 10
# Use local AI instead of OpenAI
npx git-rewrite-commits --provider ollama --max-commits 103. Install Git Hooks for automatic messages
cd your-repo
npx git-rewrite-commits --install-hooksThis installs three hooks into your repository. The prepare-commit-msg hook generates AI messages when you run git commit. The post-commit hook reviews and improves your commit message after committing. The pre-push hook offers to fix commits before pushing to the remote.
Once hooks are installed, committing looks like this:
git add .
git commit
# Your editor opens with an AI-generated commit message ready to use4. Control how the tool generates messages with the following configuration options:
# Use a custom template format
npx git-rewrite-commits --template "[JIRA-123] feat: message"
# Generate messages in Spanish
npx git-rewrite-commits --language es
# Use GPT-4 for better quality (OpenAI only)
npx git-rewrite-commits --model gpt-4
# Process all commits including well-formed ones
npx git-rewrite-commits --no-skip-well-formed
# Use a custom AI prompt for unique styles
npx git-rewrite-commits --prompt "Generate fun commit messages with relevant emojis"5. Once the tool finishes, review the changes:
git log --oneline6. If satisfied with the new messages, push to remote with force-with-lease (safer than regular force push):
git push --force-with-lease7. If something went wrong, restore from the automatically created backup branch:
git reset --hard backup-branch-name8. The tool always creates a backup before modifying history unless you explicitly skip it with the –skip-backup flag (not recommended for first-time use).
Pros
- Saves Time: It automates the often-tedious task of writing good commit messages.
- Improves Readability: A clean, conventional commit history is much easier for anyone to read and understand.
- Offline and Private: The support for Ollama means you can use it without an internet connection and without sending your code diffs to an external API.
- Highly Customizable: You can tailor the output to fit specific project requirements, from JIRA ticket formats to haiku-style commit messages.
Cons
- Destructive Operation: The most significant drawback is that it rewrites git history. This is a powerful and potentially destructive action. You should never use it on a shared main branch (like
mainormaster) that other developers pull from. - Requires Force-Pushing: After rewriting history, you must force-push your changes, which can cause problems for collaborators if not coordinated properly.
Related Resources
- Ollama: Open-source tool for running large language models locally. Required for using git-rewrite-commits offline with models like llama or mistral.
- Conventional Commits: Specification for adding human and machine-readable meaning to commit messages. Understanding this standard helps when customizing git-rewrite-commits templates.
- Pro Git Book – Rewriting History: A guide to git history manipulation. Useful background reading before using any history-rewriting tool including git-rewrite-commits.
- GitHub Guide on Commit Messages: Best practices and examples for writing good commit messages. Helps you understand what git-rewrite-commits aims to generate.
- OpenAI Platform: Where you get API keys for using git-rewrite-commits with GPT models. Also provides pricing information and usage monitoring.
FAQs
Q: Is it safe to use git-rewrite-commits on my main branch?
A: No. The tool rewrites git history, which changes commit hashes and requires force-pushing. Using it on main or master branches of team projects will disrupt everyone’s workflow. Stick to personal projects, feature branches with team agreement, or local commits before pushing. The general rule is never rewrite history that other people depend on.
Q: How much does it cost to use with OpenAI?
A: Costs depend on how many commits you process and which model you use. GPT-3.5-turbo is cheaper but produces adequate results for most commits. GPT-4 costs more but generates better quality messages. For large-scale use or cost concerns, consider running Ollama locally with free open-source models.
Q: Can I undo changes if I don’t like the rewritten history?
A: Yes. The tool automatically creates a backup branch before making any changes (unless you use –skip-backup flag). If you’re unhappy with the results, run git reset –hard backup-branch-name to restore your original history. You can also use git reflog to find previous states of your branch if needed.










