Generate high-quality git commit messages from staged changes using AI. cmg analyzes your diffs and uses Claude, ChatGPT, or any LLM to produce clear, convention-compliant messages.
- Multiple LLM providers: Claude (via CLI or API), OpenAI API, or any custom CLI tool
- Project-specific rules: Define commit conventions in a simple Markdown file per project
- Smart diff handling: Analyzes staged changes with size warnings for large diffs
- Interactive commit flow: After generation, choose to commit, edit, or cancel — no extra flags needed
- Interactive setup:
cmg initguides you through configuration - Secure config: API keys stored locally with file permissions (0600), easily ignored in git
go install github.com/kokoa-tools/commit-message-generator@latestRequires Go 1.25 or later.
cd your-git-repo
cmg initThis creates .commit-generator/config.json with your LLM provider settings. You'll be prompted to:
- Choose an LLM provider (Claude CLI, custom CLI, Anthropic API, or OpenAI API)
- Set diff size thresholds
- Optionally create project-specific commit rules
Stage your changes and run:
git add .
cmgcmg analyzes the diff, calls your LLM, and displays the generated message. You are then prompted to act:
────────────────────────────────────────────────────────────
feat: add interactive commit prompt with edit and regenerate
- Replace -c flag with automatic post-generation prompt
- Support Y/e/r/n choices for commit, edit, regenerate, cancel
- Open $EDITOR for in-place message editing
────────────────────────────────────────────────────────────
Commit with this message? [Y]es / [e]dit / [r]egenerate / [n]o:
| Key | Action |
|---|---|
Y / Enter |
Run git commit with the message |
e |
Open $EDITOR to edit the message, then re-prompt |
r |
Exit (re-run cmg to regenerate) |
n |
Cancel without committing |
cmg rules initCreate .commit-generator/message-rules.md to enforce conventions like "feat:" prefixes or emoji usage.
Generate a commit message from staged changes.
Flags:
-f, --force— Skip size warnings and proceed anyway-y, --yes— Auto-commit without prompting (useful in scripts)-d, --dry-run— Print the generated message only, skip the prompt
Examples:
# Generate and use interactive prompt (default)
cmg
# Skip size warnings
cmg --force
# Auto-commit without any prompts (scripting/CI)
cmg --yes
# Just print the message, no interaction
cmg --dry-run
# Combine: skip warnings and auto-commit
cmg -fyInitialize .commit-generator/ in your git repository.
Creates:
.commit-generator/config.jsonwith LLM provider configuration- (Optional)
.commit-generator/message-rules.mdwith project-specific conventions
Recommended: Add .commit-generator/config.json to .gitignore to avoid committing API keys.
View current configuration (API keys are masked).
cmg configModify configuration values without reinitializing.
Valid keys:
provider—claude-cli,cli,openai, oranthropiccli_command— Command name (forcliorclaude-cli)cli_args— Arguments before the prompt (space-separated)api_key— API key foropenaioranthropicmodel— Model name (e.g.,gpt-4o,claude-opus-4-6)max_diff_bytes— Threshold for byte size warning (default: 51200)max_diff_lines— Threshold for line count warning (default: 1000)
Examples:
# Switch to OpenAI
cmg config set provider openai
cmg config set api_key sk-...
cmg config set model gpt-4o
# Update diff thresholds
cmg config set max_diff_bytes 102400
cmg config set max_diff_lines 2000Display current project-specific commit rules.
cmg rulesCreate .commit-generator/message-rules.md interactively.
You'll be prompted for:
- Commit message format (e.g.,
feat: description) - Allowed types/prefixes
- Maximum subject line length
- Whether to include emoji
- Language for messages
- Additional rules or examples
Generated file is safe to commit to the repository.
Open message-rules.md in your default editor ($EDITOR or $VISUAL).
cmg rules editChoose a provider during cmg init or update it later with cmg config set provider <name>.
| Provider | Setup | Use Case | Cost |
|---|---|---|---|
| claude-cli | Install claude CLI | Free/included in CLI plan | No direct cost |
| cli | Any LLM CLI tool (e.g., kiro, ollama) |
Custom/local models | Varies |
| openai | Get API key from OpenAI | ChatGPT models | Pay-per-use |
| anthropic | Get API key from Anthropic | Claude models | Pay-per-use |
cmg init
# Choose option 1: claude-cli
# It will verify `claude` is available in PATHNo API key needed. Uses your existing Claude CLI setup.
cmg config set provider cli
cmg config set cli_command kiro
cmg config set cli_args "-p"Your command will be invoked as:
<cli_command> <cli_args> <prompt_text>
The prompt is piped as stdin.
cmg config set provider openai
cmg config set api_key sk-...
cmg config set model gpt-4ocmg config set provider anthropic
cmg config set api_key sk-ant-...
cmg config set model claude-opus-4-6Create .commit-generator/message-rules.md to enforce project conventions. The AI generator reads this file and follows all specified rules.
A simple Markdown file with sections:
# Commit Message Rules
These rules are enforced by the AI when generating commit messages for this project.
## Format
feat: short description
fix: short description
docs: short description
## Allowed Types
feat, fix, docs, style, refactor, perf, test, chore
## Subject Line
- Maximum 72 characters
- Use imperative mood ("Add" not "Added")
- Do not end with a period
## Language
Write commit messages in English.
## Examples
feat: add user authentication system
fix: resolve race condition in worker pool
docs: update README with new API endpointsUse the interactive prompt:
cmg rules initOr edit directly:
cmg rules edit- Keep rules concise and clear
- Focus on format, conventions, and language
- Include 2-3 examples
- All sections are optional; the AI uses sensible defaults if rules aren't set
Configuration is stored at .commit-generator/config.json in your git repository.
{
"provider": "claude-cli",
"cli_command": "claude",
"cli_args": ["-p"],
"api_key": "",
"model": "",
"max_diff_bytes": 51200,
"max_diff_lines": 1000
}API keys are stored in plaintext in config.json. Add it to .gitignore:
echo ".commit-generator/config.json" >> .gitignoreFile permissions are set to 0600 (readable only by the owner). To verify:
ls -la .commit-generator/config.json
# -rw------- user group ...The message-rules.md file is safe to commit — it contains no secrets.
# Initialize
cmg init
# → Choose option 1: claude-cli
# Stage and generate
git add src/auth.go
cmg
# → Review message
# → Press Y to commit, e to edit in $EDITOR, n to cancel# Initialize
cmg init
# → Choose option 4: openai
# → Enter your API key and model (gpt-4o)
# Skip warnings and auto-commit without prompts
cmg -fy# Initialize
cmg init
# → Choose option 3: anthropic
# → Enter your API key and model (claude-opus-4-6)
# Generate, then edit message before committing
cmg
# → Press e to open $EDITOR, tweak the message, save and exit
# → Press Y to commit with the edited message# Initialize
cmg init
# → Choose option 2: cli
# → Enter command: kiro
# → Enter args: (leave blank or enter custom flags)
# Generate with dry-run to preview only
cmg --dry-runYou're not in a git repository or .commit-generator/config.json doesn't exist.
cd your-repo
cmg initYou haven't staged any files yet.
git add your-changes
cmgCheck that:
- Your API key is correct:
cmg config(it will be masked) - Your API key has sufficient quota/permissions
- You're online
The claude command isn't in your PATH.
which claude
# If empty, install: https://github.com/anthropics/anthropic-cli
# Or use a custom CLI provider:
cmg config set provider cli
cmg config set cli_command /path/to/your/llm-toolIf your diff is very large, the AI may generate lower-quality messages. Either:
- Use
--forceto skip the warning - Split into smaller commits (recommended)
- Increase thresholds:
cmg config set max_diff_bytes 204800 cmg config set max_diff_lines 5000
Clone and build:
git clone https://github.com/koajs/commit-message-generator.git
cd commit-message-generator
go build -o cmg main.go
./cmg --helpRun tests:
go test ./...MIT
Contributions welcome. Submit issues and PRs to the GitHub repository.