我写了一个 AI Commit Message 生成器,再也不用想怎么写 git commit 了

0 阅读2分钟

我写了一个 AI Commit Message 生成器,再也不用想怎么写 git commit 了

cover.png

读 diff → AI 生成 → 确认 → 提交,零依赖、支持 6 大 AI 厂商、npx 即跑。

背景:你还在写 "fix bug" 吗

先来个灵魂拷问:你的 git log 长什么样?

fix bug
update
修复
wip
asdf

别装了,90% 的人都这样。

不是不想写好,是 真的懒。你改了 5 个文件、加了 200 行代码,然后要你用 Conventional Commits 格式写一条精确的 commit message——type 要对、scope 要有、body 要列出来——算了,fix bug 吧。

但 commit message 不是给你自己看的。三个月后你 git blame 的时候,看到一行 "update",你会恨死三个月前的自己。

所以我做了这个工具:ai-git-msg —— 让 AI 帮你写 commit message。

git add .
npx ai-git-msg

两行命令,读 diff → AI 生成规范的 commit message → 你确认 → 自动提交。

终端实际运行效果:

terminal-demo.png


它能做什么

$ npx ai-git-msg

📝  正在分析 staged 变更...
📂  变更统计:  3 files changed, 47 insertions(+), 12 deletions(-)
🤖  正在调用 AI 生成 commit message...
🔗  Provider: deepseek
🧠  模型: deepseek-chat

┌─────────────────────────────────────────────┐
│  🤖 Generated commit message:               │
└─────────────────────────────────────────────┘

  feat(auth): 添加 Google OAuth 登录支持

  - 新增 GoogleOAuthButton 组件处理重定向
  - 实现 OAuth 回调路由完成 token 交换
  - 更新 auth store 持久化 OAuth token

Confirm? [Y]es / [n]o / [e]dit / [r]egenerate > y

🎉  ✅ 已提交: feat(auth): 添加 Google OAuth 登录支持

四个选项:

  • Y — 直接提交
  • n — 取消
  • e — 手动编辑后提交
  • r — 不满意?让 AI 重新生成一条

30 秒上手

# 1. 配 Key(任选一个 AI 服务)
echo 'DEEPSEEK_API_KEY=sk-xxx' >> .env.local

# 2. Stage 你的改动
git add .

# 3. AI 生成 commit message
npx ai-git-msg

没了。不需要 npm install,不需要配置文件。


四种规范风格

不同团队用不同的 commit 规范,一个参数切换:

Angular(默认)

npx ai-git-msg --style angular
feat(auth): 添加 Google OAuth 登录支持

- 新增 GoogleOAuthButton 组件
- 实现 OAuth 回调路由

Conventional

npx ai-git-msg --style conventional
feat: 添加 Google OAuth 登录支持

Simple

npx ai-git-msg --style simple
添加 Google OAuth 登录支持

Gitmoji

npx ai-git-msg --style gitmoji
feat(auth): 添加 Google OAuth 登录支持

核心设计决策

为什么只处理 staged 变更

很多同类工具会读所有未提交的改动。我只读 git diff --cached(staged),原因是:

一次 commit 应该只做一件事。你 stage 了什么,就 commit 什么。

如果你改了 10 个文件但只 git add 了 3 个,AI 只分析这 3 个文件的 diff,生成的 message 会更精确。

为什么不用 Inquirer / Commander / Ora

零依赖。Node.js 18+ 内置了 readlinefetchchild_process,足够做一个 CLI 了。不需要为了一个 spinner 拉 40 个 packages 进来。

整个工具 0 个 required dependencynpx 下载 11KB 就能跑。

Diff 截断保护

如果你一次 stage 了 2000 行改动,完整的 diff 会超出 AI 的 token 限制。默认截断到 500 行,保证 prompt 不爆。可以在配置文件里调整:

{
  "maxDiffLines": 500
}

6 大 AI 厂商,自动识别

ai-review-pipeline 共享环境变量,如果你已经配过 AI Review 的 Key,这里直接能用:

OPENAI_API_KEY=sk-xxx              # → OpenAI
DEEPSEEK_API_KEY=sk-xxx            # → DeepSeek(国内推荐,便宜)
ANTHROPIC_API_KEY=sk-ant-xxx       # → Claude
DASHSCOPE_API_KEY=sk-xxx           # → 通义千问
GEMINI_API_KEY=xxx                 # → Google Gemini
AI_REVIEW_PROVIDER=ollama          # → 本地 Ollama(免费,离线可用)
Provider默认模型一次 commit 约
DeepSeekdeepseek-chat¥0.001
OpenAIgpt-4o-mini$0.001
Ollamaqwen2.5-coder免费
Claudeclaude-sonnet-4$0.003
通义千问qwen-plus¥0.002
Geminigemini-2.0-flash免费额度内

生成一条 commit message 的成本基本可以忽略不计。


配置文件

npx ai-git-msg init

生成 .ai-commit.json,提交到 git 团队共享:

{
  "style": "angular",
  "language": "zh",
  "maxDiffLines": 500,
  "scopeMap": {},
  "types": ["feat", "fix", "refactor", "docs", "style", "test", "chore", "perf", "ci"],
  "emoji": false,
  "autoConfirm": false,
  "model": ""
}
  • style — 团队统一用一种规范
  • language — commit message 用中文还是英文
  • autoConfirm — CI 场景设为 true,跳过交互直接提交

常用命令速查

# 基本用法
npx ai-git-msg                         # 分析 staged,生成 commit message

# 风格选择
npx ai-git-msg --style angular         # Angular 风格(默认)
npx ai-git-msg --style gitmoji         # Gitmoji 风格
npx ai-git-msg --style simple          # 简单一行

# 控制选项
npx ai-git-msg --lang en               # 英文 commit message
npx ai-git-msg --dry-run               # 只展示不提交
npx ai-git-msg -y                      # 跳过确认直接提交
npx ai-git-msg --scope auth            # 手动指定 scope

# 初始化
npx ai-git-msg init                    # 生成配置文件

和同类工具的对比

维度ai-git-msgaicommitscz-git
安装npx 即跑,零配置需 npm install需 npm install
依赖0 个10+20+
AI 模型6 家 + 本地 Ollama仅 OpenAI无 AI
多语言中英文 message仅英文仅英文
多风格4 种规范1 种Commitizen
重新生成✅ 不满意可 regenerate
大小11KB100KB+200KB+

工具链

这是我做的 AI 开发工具链的第二个工具:

代码编写
   │
   ├── ai-review-pipeline ── 代码质量审查
   │         npx ai-review-pipeline
   │
   ├── ai-git-msg ── 提交信息生成       ← 你在这里
   │         npx ai-git-msg
   │
   └── ai-i18n-sync ── 国际化翻译同步
              npx ai-i18n-sync

三个工具共享同一套 AI Provider 和环境变量,配一次 Key 全家用。


开源地址

git add .
npx ai-git-msg

试试看,从此告别 "fix bug"。


如果对你有帮助,给个 ⭐ 或者掘金点个赞,是我继续迭代的动力。

有问题或建议欢迎提 issue 或评论区交流。