用途
git-commit-smart 用于在提交代码变更时,自动分析已暂存(staged)的改动并生成有意义、结构化的提交信息,遵循 Conventional Commits 格式。其灵感来自 claude-code-plugins-plus。
提交信息格式
<type>(<scope>): <subject>
<body>
<footer>
类型(Type)
| 类型 | 说明 |
|---|---|
| feat | 新功能 |
| fix | 缺陷修复 |
| docs | 仅文档变更 |
| style | 格式调整,无代码逻辑变化 |
| refactor | 代码重构,不新增功能或修复 |
| perf | 性能改进 |
| test | 新增/更新测试 |
| chore | 维护类任务 |
| ci | CI/CD 变更 |
| build | 构建系统变更 |
作用域(Scope)
作用域为可选项,用于指示代码库中的具体部分,例如:
auth、api、ui、db、config 等。
主题(Subject)
- 使用祈使语气(如 "add" 而非 "added")
- 结尾不加句号
- 最多 50 个字符
- 使用小写
正文(Body)
- 说明是什么(WHAT)和为什么(WHY),而非如何做(HOW)
- 每行在 72 个字符处换行
- 与主题之间用空行分隔
页脚(Footer)
- 破坏性变更:
BREAKING CHANGE: description - 关联议题:
Closes #123、Fixes #456
工作流程
- 分析改动:
git diff --staged
git diff --staged --name-only
git diff --staged --stat
- 确定类型:
- 新增包含功能的文件 → feat
- 修改文件修复问题 → fix
- 仅 .md 文件 → docs
- 仅测试文件 → test
- package.json、配置文件 → chore 或 build
- 识别作用域(根据文件路径):
- src/auth/* → scope: auth
- src/api/* → scope: api
- tests/* → scope: test
- 涉及多个区域 → 省略作用域或使用更宽泛的术语
- 编写主题,例如:
feat(auth): add OAuth2 login support
fix(api): handle null response in user endpoint
docs: update README with installation steps
refactor(db): extract query builder to separate module
- 如有需要,编写正文,例如:
feat(auth): add OAuth2 login support
Implement OAuth2 authentication flow with support for
Google and GitHub providers. This allows users to sign
in without creating a separate account.
- Add OAuth2 client configuration
- Implement callback handlers
- Store OAuth tokens securely
- Add provider selection UI
示例
简单功能:
feat(ui): add dark mode toggle
带议题引用的缺陷修复:
fix(api): prevent duplicate user creation
Check for existing email before inserting new user record.
Race condition could cause duplicate entries when requests
arrived simultaneously.
Fixes #234
破坏性变更:
feat(api)!: change response format to JSON:API
BREAKING CHANGE: All API responses now follow JSON:API
specification. Clients must update their parsers.
Migration guide: docs/migration-v2.md
多文件、单一目的:
refactor: extract validation logic to shared module
Move duplicate validation code from controllers to
a shared validation module. Reduces code duplication
and ensures consistent validation across endpoints.
应避免的反模式
- ❌ "fix stuff"
- ❌ "update code"
- ❌ "WIP"
- ❌ "changes"
- ❌ "fixed bug"
- ❌ "misc updates"
- ❌ 包含 50 个以上文件且信息含糊的提交
最佳实践
- 原子提交:每次提交一个逻辑变更
- 有意义的信息:未来的你会感谢现在的你
- 引用议题:在相关时关联工单/议题
- 提交前审查:始终执行
git diff --staged - 不要提交机密:检查 API 密钥、密码
快速参考
# 暂存特定文件
git add <file>
# 暂存所有改动
git add -A
# 交互式暂存
git add -p
# 带信息提交
git commit -m "type(scope): subject"
# 带正文提交
git commit -m "type(scope): subject" -m "body paragraph"
# 修改上一次提交
git commit --amend
限制
关于该工具的具体安装方式、收费信息、支持的平台或运行环境,资料中暂未提供。