Git Commit Template
Create structured, changelog-style Git commit messages using five standard categories. This ensures consistent, searchable, and maintainable commit history across projects.
Commit Structure
All commits follow this format:
CODEBLOCK0
Commit Types
Added
For new features, functionality, or capabilities.
CODEBLOCK1
Changed
For changes in existing functionality, refactoring, or improvements.
CODEBLOCK2
Deprecated
For features that will be removed in future releases.
CODEBLOCK3
Removed
For removed features, code, or dependencies.
CODEBLOCK4
Fixed
For bug fixes and error corrections.
CODEBLOCK5
Quick Start
Method 1: Interactive Helper Script (Recommended)
Use the bundled Python script for guided commit creation:
CODEBLOCK6
The script will:
- 1. Show staged files
- Prompt for commit type selection
- Ask for title and optional body
- Preview and confirm before committing
Method 2: Direct Command Line
For quick commits:
CODEBLOCK7
Method 3: Manual Git Commands
Standard git workflow with template:
CODEBLOCK8
Guidelines
Title Best Practices
- - Keep under 72 characters
- Use imperative mood: "add" not "added"
- Be specific: "[Fixed] null pointer in user parser" not "[Fixed] bug"
- No period at end
- Describe what changed, not how
Body Best Practices
- - Wrap at 72 characters
- Explain why the change was made
- Include breaking changes prominently
- Reference issue numbers: "Closes #123", "Fixes #456"
- Add migration instructions for deprecations
- Only add body for complex changes
When to Use Each Type
Added: New endpoints, features, files, tests, documentation sections
Changed: Refactoring, performance improvements, API modifications, dependency updates
Deprecated: Marking features for future removal, providing migration paths
Removed: Deleting features, dropping support, removing dependencies
Fixed: Bug fixes, error corrections, security patches
Common Patterns
Breaking Changes
Always call out breaking changes:
CODEBLOCK9
Multiple Related Changes
Group related changes in one commit with bullet points:
CODEBLOCK10
Security Fixes
Be cautious with details:
CODEBLOCK11
Workflow Integration
Pre-commit Checks
The helper script automatically:
- - Verifies staged files exist
- Validates commit type
- Ensures title is non-empty
- Shows file list in confirmation
Git Hooks
Compatible with standard git hooks (pre-commit, commit-msg, etc.):
CODEBLOCK12
Searching Commit History
The structured format makes history searchable:
CODEBLOCK13
Examples Reference
For comprehensive examples, best practices, and anti-patterns, see:
- - references/examples.md - Detailed examples for each commit type with good/bad patterns
Script Features
The scripts/commit.py helper provides:
- - Interactive mode: Guided commit creation with prompts
- Direct mode: Command-line commit creation
- Validation: Type checking and staged file verification
- Preview: Show commit before creation
- Multi-line bodies: Support for detailed descriptions
- Error handling: Clear error messages and guidance
Git 提交模板
使用五个标准类别创建结构化、变更日志风格的 Git 提交信息。这确保了跨项目的一致、可搜索和可维护的提交历史。
提交结构
所有提交遵循以下格式:
[类型] 简短描述性标题(不超过72个字符)
可选正文,包含详细说明、实现说明、
破坏性变更或迁移指南。
提交类型
新增
用于新功能、特性或能力。
bash
git commit -m [新增] 使用JWT的用户认证 -m 实现了基于JWT的认证,包含登录/登出端点和令牌刷新
变更
用于现有功能的变更、重构或改进。
bash
git commit -m [变更] 改进数据库查询性能 -m 添加了复合索引,查询时间从250ms降低到15ms
弃用
用于将在未来版本中移除的功能。
bash
git commit -m [弃用] 旧版API v1端点 -m 将在v3.0中移除。迁移指南:docs/migration.md
移除
用于已移除的功能、代码或依赖。
bash
git commit -m [移除] 对Python 3.7的支持 -m 最低版本现在为3.8。已更新CI/CD和文档。
修复
用于错误修复和纠错。
bash
git commit -m [修复] WebSocket处理器中的内存泄漏 -m 修复了未关闭的连接导致无限制内存增长的问题。关闭 #1234
快速开始
方法1:交互式辅助脚本(推荐)
使用捆绑的Python脚本进行引导式提交创建:
bash
python scripts/commit.py
该脚本将:
- 1. 显示暂存的文件
- 提示选择提交类型
- 询问标题和可选正文
- 在提交前预览并确认
方法2:直接命令行
用于快速提交:
bash
仅标题
python scripts/commit.py 新增 用户个人资料页面
标题加正文
python scripts/commit.py 修复 登录超时 将会话超时从5分钟增加到15分钟
方法3:手动Git命令
使用模板的标准Git工作流程:
bash
暂存更改
git add src/auth.py tests/test_auth.py
使用模板提交
git commit -m [新增] 双因素认证 \
-m 实现了基于TOTP的双因素认证,包含二维码生成和备份码
指南
标题最佳实践
- - 保持在72个字符以内
- 使用祈使语气:添加而非已添加
- 具体明确:[修复] 用户解析器中的空指针而非[修复] 错误
- 末尾不加句号
- 描述更改了什么,而非如何更改
正文最佳实践
- - 每行72个字符换行
- 解释为什么进行更改
- 突出显示破坏性变更
- 引用问题编号:关闭 #123,修复 #456
- 为弃用添加迁移指南
- 仅对复杂更改添加正文
何时使用每种类型
新增:新端点、功能、文件、测试、文档章节
变更:重构、性能改进、API修改、依赖更新
弃用:标记未来将移除的功能,提供迁移路径
移除:删除功能、取消支持、移除依赖
修复:错误修复、纠错、安全补丁
常见模式
破坏性变更
始终指出破坏性变更:
[变更] 重构API响应格式
破坏性变更:响应结构已修改。
旧版:{ user: {...} }
新版:{ data: {...}, meta: {...} }
迁移:访问 response.data 而非 response.user
多个相关更改
使用项目符号将相关更改分组到一个提交中:
[新增] 全面日志系统
- - 带上下文的结构化日志
- 日志轮转和归档
- 监控仪表板集成
- 性能指标收集
安全修复
谨慎处理细节:
[修复] 认证绕过漏洞
修复了令牌验证问题。根据负责任披露政策,
详情暂不公开。请参阅安全公告SA-2024-001。
工作流程集成
提交前检查
辅助脚本自动:
- - 验证暂存文件是否存在
- 验证提交类型
- 确保标题非空
- 在确认时显示文件列表
Git钩子
与标准Git钩子(pre-commit、commit-msg等)兼容:
bash
如果需要,使用 --no-verify 跳过钩子
python scripts/commit.py 修复 紧急热修复 --no-verify
搜索提交历史
结构化格式使历史可搜索:
bash
查找所有新功能
git log --oneline --grep=^\[新增\]
查找所有错误修复
git log --oneline --grep=^\[修复\]
生成变更日志
git log --oneline --grep=^\[新增\]\|^\[变更\]\|^\[修复\]
示例参考
有关全面的示例、最佳实践和反模式,请参阅:
- - references/examples.md - 每种提交类型的详细示例,包含好/坏模式
脚本功能
scripts/commit.py 辅助脚本提供:
- - 交互模式:带提示的引导式提交创建
- 直接模式:命令行提交创建
- 验证:类型检查和暂存文件验证
- 预览:在创建前显示提交
- 多行正文:支持详细描述
- 错误处理:清晰的错误信息和指导