Important safety warning for rewriting Git history
mastergit-rewrite-commits to ensure you can recover your work if the rewrite does not produce the intended results.repository·master·Indexed 23 days ago
https://github.com/f/git-rewrite-commitsAn AI-powered tool for rewriting git commit history and generating high-quality conventional commit messages using OpenAI or local models via Ollama. It includes a CLI (also available as the 'grec' alias) and opt-in git hooks (pre-commit and prepare-commit-msg) to automate message generation while protecting sensitive data like API keys and passwords.
git-rewrite-commits to ensure you can recover your work if the rewrite does not produce the intended results.The tool installs three specific hooks to manage the AI commit lifecycle:
prepare-commit-msg: Triggered during git commit. It generates the AI message based on staged changes and opens your editor with the message ready.post-commit: Triggered after a successful commit. It reviews and improves the commit message, automatically fixing poor ones.pre-push: Triggered before git push. It reviews unpushed commits and offers to fix messages before they are sent to the remote.This tool rewrites git history, which is generally NOT recommended for shared repositories. Rewriting history changes commit hashes and requires force-pushing.
Recommended use cases:
When using remote AI providers (like OpenAI), your file lists and diffs are sent to external APIs. For sensitive repositories, it is highly recommended to use Ollama for local processing so that no data leaves your machine.
You can enforce consistent commit message formats using the template option. Supported variables include:
{type}: Commit type (feat, fix, docs, etc.){scope}: Commit scope (optional){description}: Commit description{ticket}: Extracted ticket number (if found)const rewriter = new GitCommitRewriter({
template: '[JIRA-{ticket}] {type}: {description}'
});
// Generates: "[JIRA-123] feat: add user authentication"The project provides two intelligent, coordinated Git hooks that work together to provide AI-powered commit messages while minimizing API usage and duplication:
pre-commit: A preview hook. It shows a suggested AI-generated message and asks for confirmation. If you confirm, it saves the message so the next hook doesn't have to regenerate it. It can replace a manual -m message if you approve the suggestion.prepare-commit-msg: An automation hook. It runs automatically when you commit without a message. It reuses the message from pre-commit if one was already generated, or analyzes staged changes to generate a new one. It inserts the message into your editor for final review.Key Coordination Rules:
prepare-commit-msg respects decisions made during the pre-commit phase.The tool is designed with a privacy-first approach to protect sensitive data during the AI rewriting process.
Automatic Protection:
.env files are completely hidden from the AI.Privacy Controls:
To guide the AI with project-specific rules (e.g., specific scopes, ticket number requirements, or security emphasis), create a COMMIT_MESSAGE.md file. The tool searches for this file in the following order:
./COMMIT_MESSAGE.md./.git/COMMIT_MESSAGE.md./.github/COMMIT_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.You can use the tool without permanent installation using npx, or install it globally for easier access. The short alias grec is identical to the full command name git-rewrite-commits.
# Quick Start (No Installation Required)
npx git-rewrite-commits
# or shorter:
npx grec
# Global Installation
npm install -g git-rewrite-commits
# or shorter:
npm install -g grecThe grec command is a drop-in alias for git-rewrite-commits. You can use it to rewrite commit history using AI, preview changes, or manage git hooks.
Common tasks include:
grec to start the AI-powered rewriting process.--dry-run to preview how commits will be changed without actually applying them.--install-hooks to automate the process.--staged to generate a message based on currently staged changes.--provider ollama to use a local Ollama instance instead of a remote API.# Rewrite commit history with AI
grec
# Preview changes (dry run)
grec --dry-run
# Install git hooks
grec --install-hooks
# Generate message for staged changes
grec --staged
# Use local AI with Ollama
grec --provider ollamaYou can install the AI-powered hooks using the recommended built-in installer or manually.
This works on all platforms and automatically handles backups of existing hooks.
npx git-rewrite-commits --install-hookscp hooks/pre-commit .git/hooks/
cp hooks/prepare-commit-msg .git/hooks/
chmod +x .git/hooks/*copy hooks\pre-commit.bat .git\hooks\pre-commit
copy hooks\prepare-commit-msg.bat .git\hooks\prepare-commit-msgFor security, hooks are opt-in. You must explicitly enable them via git config after installation:
# Enable preview before commit
git config hooks.preCommitPreview true
# Enable automatic message generation
git config hooks.prepareCommitMsg trueWhen working with highly sensitive code, follow these three steps to minimize risk:
The GitCommitRewriter class is the primary entry point for programmatic usage. You can use it to rewrite existing git history or generate new commit messages for staged changes.
import { GitCommitRewriter } from 'git-rewrite-commits';
const rewriter = new GitCommitRewriter({
provider: 'openai', // or 'ollama'
apiKey: process.env.OPENAI_API_KEY,
model: 'gpt-4', // optional, defaults to gpt-3.5-turbo
dryRun: false,
verbose: true
});
// Rewrite history
await rewriter.rewriteHistory();
// Generate message for staged changes
const message = await rewriter.generateForStaged();
console.log(message);