文档命令库智能提交信息

智能提交信息

让 Claude Code 分析你的代码变更,自动生成符合约定式提交(Conventional Commits)规范的提交信息,提升项目历史的可读性。

适用场景

  • 提交信息写作困难或不规范
  • 团队需要统一的提交信息格式
  • 自动化版本发布和变更日志生成
  • 提升代码审查和项目维护效率

复制命令

# 基础智能提交
分析当前的代码变更并生成合适的提交信息
 
# 指定提交类型
分析变更并生成feat类型的提交信息,包含详细描述
 
# 多文件变更分析
为暂存区的所有变更生成结构化的提交信息
 
# 包含破坏性变更
分析变更并标注是否包含BREAKING CHANGES

执行步骤

1. 代码变更分析

Claude Code 会自动:

  • 分析 git diff --cached 的变更内容
  • 识别文件类型和变更性质(新增、修改、删除)
  • 理解业务逻辑变更的影响范围
  • 检测是否包含破坏性变更

2. 提交类型识别

根据变更内容自动判断提交类型:

# 新功能
feat: add user authentication system
 
# Bug 修复
fix: resolve memory leak in data processing
 
# 文档更新
docs: update API documentation for v2.0
 
# 样式调整
style: format code with prettier
 
# 重构
refactor: extract user service into separate module
 
# 性能优化
perf: optimize database query performance
 
# 测试添加
test: add unit tests for user service
 
# 构建相关
build: update webpack configuration
 
# CI 配置
ci: add GitHub Actions workflow
 
# 其他变更
chore: update dependencies

3. 提交信息生成

生成符合规范的提交信息:

标准格式

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

实际示例

# 简单提交
feat: add dark mode toggle
 
# 详细提交
feat(auth): implement OAuth2 login flow
 
Add support for Google and GitHub OAuth2 providers.
This enables users to log in without creating accounts.
 
- Add OAuth2 client configuration
- Implement callback handlers
- Update user model to support external IDs
 
Closes #123

4. 执行提交

# Claude Code会提供完整的提交命令
git add .
git commit -m "feat(components): add responsive navigation menu
 
Implement mobile-friendly navigation with hamburger menu.
The navigation automatically adapts to screen sizes and
includes smooth transitions and accessibility features.
 
- Add mobile breakpoint detection
- Implement hamburger menu animation
- Add ARIA labels for screen readers
- Update navigation styles for mobile
 
Closes #45"

验证命令

# 查看生成的提交信息
git log --oneline -5
 
# 验证提交信息格式
git log --pretty=format:"%s" -1
 
# 检查是否符合约定式提交规范
npx commitlint --from=HEAD~1

提交类型详解

功能相关

  • feat: 新功能添加
  • fix: Bug 修复
  • perf: 性能优化

维护相关

  • refactor: 代码重构
  • style: 代码格式化
  • test: 测试相关

工程相关

  • build: 构建系统
  • ci: 持续集成
  • chore: 其他维护
  • docs: 文档更新

破坏性变更

# 包含破坏性变更的提交
feat!: remove deprecated API endpoints
 
BREAKING CHANGE: The legacy v1 API endpoints have been removed.
Users must migrate to v2 API before upgrading.
 
Migration guide: docs/migration-v2.md

💡 语义化版本:遵循约定式提交规范可以自动生成版本号和变更日志,提升项目维护效率。

高级功能

作用域识别

# 自动识别变更作用域
feat(auth): add two-factor authentication
fix(database): resolve connection timeout issue
docs(api): update authentication endpoints
style(components): apply consistent button styling

关联Issue引用

# 自动添加Issue引用
feat: implement user profile editing
 
Add comprehensive user profile editing functionality
including avatar upload and personal information updates.
 
Implements #234, Closes #156

共同作者识别

# 识别协作提交
feat: redesign dashboard layout
 
Co-authored-by: Jane Doe <jane@example.com>
Co-authored-by: John Smith <john@example.com>

提交信息质量标准

好的提交信息特征

简洁明确:在50字内说明变更内容 ✅ 使用祈使句:如”add feature”而非”added feature”
说明原因:解释为什么做这个变更 ✅ 包含影响:说明变更的影响范围

避免的模式

模糊描述:“fix bug”, “update code” ❌ 过于技术化:只有开发者能理解的术语 ❌ 缺乏上下文:没有说明变更的业务价值 ❌ 格式不一致:不遵循团队约定

团队协作配置

提交信息模板

# .gitmessage 模板文件
# <type>[optional scope]: <description>
# 
# [optional body]
#
# [optional footer(s)]
# 
# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore

提交钩子验证

# .husky/commit-msg
#!/bin/sh
npx --no-install commitlint --edit $1

自动化版本发布

# 配合 semantic-release 自动发布
npm install --save-dev semantic-release

最佳实践

提交频率

  • 小而频繁:每个提交只包含一个逻辑变更
  • 原子性提交:每个提交都应该是完整可用的
  • 测试通过:提交前确保所有测试通过

描述质量

  • 业务价值:说明变更带来的价值
  • 技术细节:在body中提供实现细节
  • 相关信息:引用相关的Issue或文档

风险提示

注意事项

  • 自动生成的提交信息仍需人工审查
  • 复杂变更可能需要手动调整描述
  • 敏感变更应避免在提交信息中暴露细节

回滚方案

# 修改最后一次提交信息
git commit --amend -m "corrected commit message"
 
# 修改历史提交信息
git rebase -i HEAD~3
 
# 重置到之前的提交
git reset --soft HEAD~1

相关卡片

参考来源

关于我