GREC - Git Rewrite Commits: Fix Your Messy Commit History with AI

Open LLM-readable version of this post Open translated version of this post

I just open sourced git-rewrite-commits, a tool that uses AI to fix your messy git commit messages automatically. Here's how it can save you from embarrassing commit histories before going public.

GREC - Git Rewrite Commits: Fix Your Messy Commit History with AI

You know that feeling when you’re about to make your private repo public and you realize half your commits say things like “fix stuff”, “asdfasdf”, or “WIP”? Yeah, I’ve been there too many times. That’s why I built git-rewrite-commits - a tool that uses AI to automatically fix your messy commit history.

I just open sourced this project, and I’m really excited to share it with you. You can start using it right now with just npx git-rewrite-commits (or the shorter alias npx grec). No installation needed!

The Problem We All Have

Let’s be honest about our commit messages. When I’m deep in the flow of coding, especially on personal projects, my commit messages look something like this:

  • “fix bug”
  • “update”
  • “finally works!!!”
  • “ugh”
  • “test”
  • “more changes”

This is fine when it’s just me working on the project. But then comes that moment when you want to share your code with the world, apply for a job, or contribute to open source. Suddenly, those commit messages are really embarrassing.

We all write bad commit messages when we’re in the zone. The problem is when those messages become permanent history.

I used to spend hours manually rewriting commit messages with git rebase -i. It was painful, time-consuming, and I’d often mess up the rebase and have to start over. There had to be a better way.

Enter AI-Powered Commit Rewriting

My solution is simple: let AI analyze your code changes and write proper commit messages for you. The tool looks at your diffs, understands what you actually changed, and generates conventional commit messages that make sense.

Here’s what it does:

  • Analyzes your staged changes or entire commit history
  • Uses AI (OpenAI or local Ollama) to understand what you did
  • Generates proper conventional commits (feat, fix, chore, etc.)
  • Can rewrite your entire git history in one command

The best part? It’s smart enough to skip commits that are already well-formed. So if you have some good commits mixed in with the bad ones, it won’t touch them. And it doesn’t use AI to understand well-formed commits. Just some pattern matching.

How I Use It

Quick Fix Before Push

When I’m about to push my feature branch, I often run:

npx grec --max-commits 5 --dry-run

This shows me what the last 5 commits would look like after AI rewrites them. If I like what I see, I run it for real:

npx grec --max-commits 5

The One-Liner Magic

Before we dive into hooks, here’s my favorite trick - you can use GREC directly in your commit command:

git commit -m "$(npx grec --staged --skip-remote-consent)"

This generates an AI commit message on the fly without any setup! The --skip-remote-consent flag skips the privacy prompt since you’re explicitly running it. It’s perfect for when you want AI messages without installing hooks.

I actually use this as an alias:

# Add to your .bashrc or .zshrc
alias gcai='git commit -m "$(npx grec --staged --skip-remote-consent)"'

# Now just type:
gcai

Boom! Instant AI commit messages with zero configuration.

The Magic of Git Hooks

But if you want it automatic, I’ve also added git hooks that generate AI commit messages every time you commit. No more thinking about commit messages!

Setting it up is super easy:

# Install the hooks
npx grec --install-hooks

# Enable the one you want
git config hooks.prepareCommitMsg true

# That's it!

Now every time I commit, I get an AI-generated message in my editor. If I don’t like it, I can edit it. But honestly, 90% of the time the AI message is better than what I would have written.

Local AI for Privacy

One thing that worried people about this project is privacy. I don’t always want to send my code to OpenAI, especially for work projects or sensitive code. That’s why I added support for Ollama - you can run the AI locally on your machine.

# Use local AI instead
npx grec --provider ollama

# Or set it as default for hooks
git config hooks.commitProvider ollama

Your code never leaves your machine, but you still get great commit messages. I use this for all my work projects.

Real Examples

Let me show you some real transformations from projects I’ve cleaned up:

Before:

"fix"
"update readme"
"stuff"
"works now"

After:

"fix: resolve null pointer exception in user service"
"docs: update README with installation instructions"
"refactor: extract validation logic to separate module"
"feat: add pagination support to API endpoints"

The AI actually looks at what you changed and writes messages that describe it. It’s not just adding random prefixes - it understands your code.

The Technical Bits

For those interested in how it works under the hood:

  1. Diff Analysis: The tool runs git diff to get your changes
  2. AI Processing: Sends the diff to OpenAI GPT or local Ollama
  3. Smart Rewriting: Uses git filter-branch to rewrite history
  4. Safety First: Always creates a backup branch before rewriting

The prompt engineering was the tricky part. I spent a lot of time fine-tuning the prompts to generate messages that feel natural but follow conventional commit standards. The AI needs to understand not just what changed, but why it matters.

Why This Matters

Good commit messages aren’t just about looking professional (though that helps). They’re about creating a history that tells a story. When you’re debugging six months later, “fix: resolve race condition in auth flow” is way more helpful than “fix bug”.

This tool has honestly changed how I work. I no longer stress about commit messages while coding. I just commit with whatever comes to mind, knowing I can clean it up later. It’s liberating.

The best tool is the one that removes friction from your workflow. This removes the friction of writing good commit messages.

Some Important Warnings

Before you rush to rewrite all your repos, there are some things you should know:

Never rewrite shared history! If other people have pulled your commits, rewriting history will cause problems. This tool is best for:

  • Personal projects before going public
  • Feature branches before merging
  • Local commits before pushing

Also, when using OpenAI, your code is sent to their API. That’s why I recommend using Ollama for sensitive projects. The tool does redact obvious secrets like API keys, but it’s better to be safe.

Addressing the Hacker News Concerns

So the project hit Hacker News and there were some valid concerns raised. Let me be honest - most of the comments are absolutely right. This is an experimental project that plays with your git history, which is generally a terrible idea. But here’s the thing: it actually works really well for specific use cases.

The biggest concern was about sending code to external APIs. Yeah, that’s a real issue. That’s why I built in several safety features:

Diff Redaction: Before any diff is sent to an AI provider, the tool scans for and redacts obvious secrets. It looks for:

  • API keys and tokens
  • Private keys and certificates
  • Password patterns
  • Environment variables that look sensitive

Here’s what happens when it finds something:

# Your diff contains:
API_KEY=sk-1234567890abcdef

# What gets sent to AI:
API_KEY=[REDACTED]

Is this perfect? No. Could something slip through? Possibly. That’s why I always recommend using Ollama for anything sensitive.

“This is a terrible idea” - Half of Hacker News, probably

And you know what? They’re not wrong! Rewriting git history IS generally a terrible idea. But sometimes terrible ideas solve real problems.

Here’s my take on the main concerns:

“You’re destroying valuable history” - True, if you use it wrong. That’s why it creates backups and why you should only use it on private branches or before going public.

“Commit messages should be written properly the first time” - In an ideal world, yes. In reality, when I’m debugging at 2 AM, my commits are going to be garbage. This tool is for the real world, not the ideal one.

“AI doesn’t understand context” - Also true! The AI sees the diff, not your thought process. But honestly? A commit message that says “refactor: extract user validation logic” is still better than “asdfasdf”.

“This encourages bad habits” - Maybe. Or maybe it removes friction so developers can focus on coding. I’d rather have developers committing frequently with bad messages (that get fixed later) than committing rarely with “perfect” messages.

Well, this is an experimental tool. It’s not for everyone, and it’s definitely not for every situation. But for those specific cases where you need to clean up a messy history before sharing, it’s incredibly useful. Use it wisely, use it carefully, and always keep backups.

Why Not Just Use AI Editors?

“But wait,” you might say, “Copilot, Windsurf, Cursor and other AI editors already have commit message generation!”

True! I use these myself. But here’s what those built-in features are missing:

Bulk History Rewriting: AI editors can generate messages for new commits, but they can’t rewrite your entire git history. When you have 50 commits that say “wip”, Cursor won’t help you fix them retroactively. GREC can rewrite months of bad commits in one go.

CLI Integration: GREC works anywhere - in your terminal, in CI/CD pipelines, in scripts. You don’t need to open an editor. Just run npx grec and you’re done. This is huge for automation and quick fixes.

Editor Independence: Not everyone uses Cursor or Windsurf. Maybe your team uses VS Code, Vim, or IntelliJ. GREC works with any setup because it’s not tied to a specific editor.

Selective Processing: You can target specific commits (--max-commits 5), skip well-formed ones (--skip-well-formed), or process only a specific branch. AI editors typically work on a per-commit basis as you create them.

Custom Templates and Languages: Need commits in Spanish? Want to follow your company’s specific format? GREC lets you customize everything with --template and --language flags. Most AI editors have fixed formats.

Local AI Options: While some AI editors are starting to support local models, GREC has first-class Ollama support. You control exactly which model runs and where your data goes.

Dry Run Mode: Preview all changes before applying them. See exactly what your history will look like after rewriting. AI editors generate and apply immediately.

Here’s a real scenario where GREC shines: You’ve been working on a side project for 6 months with terrible commits. Now a company wants to acquire it or you want to open source it. You can’t retroactively fix those commits in AI code editor. But with GREC:

# See what would change
npx grec --dry-run

# Fix everything at once
npx grec

# Force push your clean history
git push --force-with-lease

Don’t get me wrong - I love the commit generation in AI editors for day-to-day work. But GREC solves a different problem: fixing the mess you already made. They’re complementary tools, not competitors.

Installation and Getting Started

You don’t even need to install it globally. Just run:

# One-time use
npx git-rewrite-commits

# Or use the short alias
npx grec

But if you want it installed:

# Install globally
npm install -g git-rewrite-commits

# Now you can use it anywhere
grec --help

For the AI provider, you’ll need either:

  • An OpenAI API key (set as OPENAI_API_KEY environment variable)
  • Or Ollama running locally (just run ollama serve)

My Favorite Features

After using this tool for a while, here are the features I love most:

Dry Run Mode: Always preview changes before applying them. This saved me so many times from accidentally messing up my history.

Custom Prompts: You can override the AI behavior with your own prompts. Want commits in Spanish? Or following your team’s specific format? Just use --prompt.

Progress Tracking: When rewriting hundreds of commits, you get a nice progress bar. It’s the little things that make a tool pleasant to use.

Automatic Backups: Before any rewrite, it creates a backup branch. If something goes wrong, your original history is safe.

Try It Out!

I’d love for you to try git-rewrite-commits and let me know what you think. It’s completely free and open source. You can find it on GitHub at github.com/f/git-rewrite-commits.

Start with something simple:

# Try it on your current repo
npx grec --staged

This will generate a commit message for your staged changes without actually committing anything. It’s a safe way to see what the AI would write.

Conclusion

Writing good commit messages is important, but it shouldn’t slow you down. With git-rewrite-commits, you can code in the flow state without worrying about messages, then clean everything up before sharing.

This tool has become an essential part of my workflow. It lets me focus on what I love (writing code) while AI handles what I don’t (writing commit messages). And when I do want to write my own messages, I still can - the tool just gives me a better starting point.

Give it a try and let me know how it works for you. I’m always looking for feedback and ideas to make it better. After all, the best tools are built by developers, for developers.

This article was proofread and edited with AI assistance.

Cookies