git-conventions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Conventions Skill
Git 约定技能指南
This skill provides comprehensive guidance on git conventions, workflow best practices, and standardized commit formats to maintain clean, readable repository history.
本技能提供Git约定、工作流最佳实践及标准化提交格式的全面指导,以维护清晰、易读的仓库历史。
When to Use
适用场景
Activate this skill when:
- Writing commit messages following standards
- Establishing team git workflows
- Setting up branch naming conventions
- Implementing Conventional Commits
- Creating changelog automation
- Code review for git hygiene
- Onboarding team members on git practices
在以下场景中启用本技能:
- 撰写符合标准的提交消息
- 建立团队Git工作流
- 设置分支命名约定
- 实施Conventional Commits规范
- 实现变更日志自动化
- 针对Git规范的代码评审
- 为团队成员进行Git实践培训
Conventional Commits
Conventional Commits
Format
格式
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]<type>[optional scope]: <description>
[optional body]
[optional footer(s)]Commit Types
提交类型
Primary Types:
- feat: New feature for the user
- fix: Bug fix for the user
- docs: Documentation only changes
- style: Code style changes (formatting, missing semi-colons, etc)
- refactor: Code change that neither fixes a bug nor adds a feature
- perf: Performance improvements
- test: Adding or correcting tests
- build: Changes to build system or dependencies
- ci: Changes to CI configuration files and scripts
- chore: Other changes that don't modify src or test files
- revert: Reverts a previous commit
主要类型:
- feat:为用户新增功能
- fix:修复用户侧的Bug
- docs:仅修改文档
- style:代码样式变更(如格式化、补充分号等)
- refactor:既不修复Bug也不新增功能的代码变更
- perf:性能优化
- test:新增或修正测试用例
- build:构建系统或依赖项变更
- ci:CI配置文件或脚本变更
- chore:其他不修改源码或测试文件的变更
- revert:回滚之前的提交
Examples
示例
Simple commit:
bash
feat: add user authentication
Implement JWT-based authentication system with refresh tokens.
Includes middleware for protected routes.
Closes #123Breaking change:
bash
feat!: redesign API response format
BREAKING CHANGE: API now returns data in camelCase instead of snake_case.
Migration guide available in docs/migration-v2.md.
Refs: #456With scope:
bash
fix(auth): resolve token expiration edge case
Token validation now properly handles timezone offsets.
Adds retry logic for expired tokens within 5-minute grace period.Multiple paragraphs:
bash
refactor(database): optimize query performance
- Add indexes on frequently queried columns
- Implement connection pooling
- Cache common queries with Redis
- Reduce N+1 queries in user associations
Performance improved by 60% in production testing.
Reviewed-by: Jane Doe <jane@example.com>
Refs: #789简单提交:
bash
feat: add user authentication
Implement JWT-based authentication system with refresh tokens.
Includes middleware for protected routes.
Closes #123破坏性变更:
bash
feat!: redesign API response format
BREAKING CHANGE: API now returns data in camelCase instead of snake_case.
Migration guide available in docs/migration-v2.md.
Refs: #456带作用域的提交:
bash
fix(auth): resolve token expiration edge case
Token validation now properly handles timezone offsets.
Adds retry logic for expired tokens within 5-minute grace period.多段落提交:
bash
refactor(database): optimize query performance
- Add indexes on frequently queried columns
- Implement connection pooling
- Cache common queries with Redis
- Reduce N+1 queries in user associations
Performance improved by 60% in production testing.
Reviewed-by: Jane Doe <jane@example.com>
Refs: #789Commit Message Rules
提交消息规则
-
Subject line:
- Use imperative mood ("add" not "added" or "adds")
- No capitalization of first letter
- No period at the end
- Maximum 50 characters (soft limit)
- Separate from body with blank line
-
Body:
- Wrap at 72 characters
- Explain what and why, not how
- Use bullet points for multiple items
- Reference issues and PRs
-
Footer:
- Breaking changes start with "BREAKING CHANGE:"
- Reference issues: "Closes #123", "Fixes #456", "Refs #789"
- Co-authors: "Co-authored-by: Name <email>"
-
主题行:
- 使用祈使语气(用“add”而非“added”或“adds”)
- 首字母无需大写
- 结尾不加句号
- 最多50个字符(软限制)
- 与正文之间用空行分隔
-
正文:
- 每行不超过72个字符
- 说明变更内容及原因,而非实现方式
- 多项内容使用项目符号
- 关联相关议题和拉取请求
-
页脚:
- 破坏性变更以“BREAKING CHANGE:”开头
- 关联议题:“Closes #123”、“Fixes #456”、“Refs #789”
- 共同作者:“Co-authored-by: Name <email>”
Branch Naming Conventions
分支命名约定
Format Pattern
格式模板
<type>/<issue-number>-<short-description><type>/<issue-number>-<short-description>Branch Types
分支类型
Common prefixes:
- or
feature/- New featuresfeat/ - or
fix/- Bug fixesbugfix/ - - Urgent production fixes
hotfix/ - - Release preparation
release/ - - Documentation updates
docs/ - - Code refactoring
refactor/ - - Test additions or fixes
test/ - - Maintenance tasks
chore/ - or
experimental/- Proof of conceptsspike/
常见前缀:
- 或
feature/- 新功能分支feat/ - 或
fix/- Bug修复分支bugfix/ - - 紧急生产修复分支
hotfix/ - - 版本发布准备分支
release/ - - 文档更新分支
docs/ - - 代码重构分支
refactor/ - - 测试新增或修复分支
test/ - - 维护任务分支
chore/ - 或
experimental/- 概念验证分支spike/
Examples
示例
bash
undefinedbash
undefinedFeature branches
Feature branches
feature/123-user-authentication
feat/456-add-payment-gateway
feature/oauth-integration
feature/123-user-authentication
feat/456-add-payment-gateway
feature/oauth-integration
Bug fix branches
Bug fix branches
fix/789-resolve-memory-leak
bugfix/login-redirect-loop
fix/456-null-pointer-exception
fix/789-resolve-memory-leak
bugfix/login-redirect-loop
fix/456-null-pointer-exception
Hotfix branches
Hotfix branches
hotfix/critical-security-patch
hotfix/production-database-issue
hotfix/critical-security-patch
hotfix/production-database-issue
Release branches
Release branches
release/v1.2.0
release/2024-Q1
release/v1.2.0
release/2024-Q1
Documentation branches
Documentation branches
docs/api-reference-update
docs/123-add-contributing-guide
docs/api-reference-update
docs/123-add-contributing-guide
Refactor branches
Refactor branches
refactor/database-layer
refactor/456-simplify-auth-flow
refactor/database-layer
refactor/456-simplify-auth-flow
Experimental branches
Experimental branches
experimental/graphql-api
spike/performance-optimization
undefinedexperimental/graphql-api
spike/performance-optimization
undefinedBranch Naming Rules
分支命名规则
- Use hyphens for word separation (not underscores)
- Lowercase only (avoid capitals)
- Keep it short but descriptive (max 50 characters)
- Include issue number when applicable
- Avoid special characters except hyphens and forward slashes
- No trailing slashes
- Be consistent within your team
- 使用连字符分隔单词(不使用下划线)
- 全部小写(避免大写字母)
- 简短但具有描述性(最多50个字符)
- 适用时包含议题编号
- 避免特殊字符,仅允许连字符和斜杠
- 末尾不加斜杠
- 团队内部保持一致
Protected Branch Strategy
受保护分支策略
Main Branches
主要分支
main/master:
- Production-ready code
- Always deployable
- Protected with required reviews
- No direct commits
- Merge only from release or hotfix branches
develop:
- Integration branch for features
- Pre-production testing
- Protected with CI checks
- Merge target for feature branches
staging:
- Pre-production environment
- QA testing branch
- Mirror of production with new features
main/master:
- 生产就绪代码
- 始终可部署
- 启用必需评审保护
- 禁止直接提交
- 仅允许从release或hotfix分支合并
develop:
- 功能集成分支
- 预生产测试分支
- 启用CI检查保护
- 作为feature分支的合并目标
staging:
- 预生产环境分支
- QA测试分支
- 与生产环境镜像一致,包含新功能
Protection Rules
保护规则
yaml
undefinedyaml
undefinedExample GitHub branch protection
Example GitHub branch protection
main:
require_pull_request_reviews:
required_approving_review_count: 2
dismiss_stale_reviews: true
require_code_owner_reviews: true
require_status_checks:
strict: true
contexts:
- continuous-integration
- code-quality
- security-scan
enforce_admins: true
require_linear_history: true
allow_force_pushes: false
allow_deletions: false
undefinedmain:
require_pull_request_reviews:
required_approving_review_count: 2
dismiss_stale_reviews: true
require_code_owner_reviews: true
require_status_checks:
strict: true
contexts:
- continuous-integration
- code-quality
- security-scan
enforce_admins: true
require_linear_history: true
allow_force_pushes: false
allow_deletions: false
undefinedSemantic Versioning
语义化版本控制
Version Format
版本格式
MAJOR.MINOR.PATCH[-prerelease][+build]Examples:
- - Initial release
1.0.0 - - Minor update with patches
1.2.3 - - Pre-release alpha
2.0.0-alpha.1 - - Release candidate with build metadata
1.5.0-rc.2+20240321
MAJOR.MINOR.PATCH[-prerelease][+build]示例:
- - 初始版本
1.0.0 - - 包含补丁的小版本更新
1.2.3 - - Alpha预发布版本
2.0.0-alpha.1 - - 带构建元数据的候选发布版本
1.5.0-rc.2+20240321
Version Increment Rules
版本递增规则
MAJOR (X.0.0):
- Breaking changes
- API incompatibilities
- Major redesigns
- Removal of deprecated features
MINOR (x.Y.0):
- New features (backward compatible)
- Deprecated features (still functional)
- Substantial internal changes
PATCH (x.y.Z):
- Bug fixes
- Security patches
- Performance improvements
- Documentation updates
MAJOR(X.0.0):
- 破坏性变更
- API不兼容
- 重大重构
- 移除已废弃功能
MINOR(x.Y.0):
- 新增向后兼容的功能
- 标记功能为废弃(仍可正常使用)
- 大量内部变更
PATCH(x.y.Z):
- Bug修复
- 安全补丁
- 性能优化
- 文档更新
Git Tags for Versions
Git版本标签
bash
undefinedbash
undefinedCreate annotated tag
Create annotated tag
git tag -a v1.2.3 -m "Release version 1.2.3
- Add user authentication
- Fix memory leak in cache
- Improve API performance"
git tag -a v1.2.3 -m "Release version 1.2.3
- Add user authentication
- Fix memory leak in cache
- Improve API performance"
Push tags to remote
Push tags to remote
git push origin v1.2.3
git push origin v1.2.3
Push all tags
Push all tags
git push --tags
git push --tags
Create pre-release tag
Create pre-release tag
git tag -a v2.0.0-beta.1 -m "Beta release for v2.0.0"
git tag -a v2.0.0-beta.1 -m "Beta release for v2.0.0"
Delete tag
Delete tag
git tag -d v1.2.3
git push origin :refs/tags/v1.2.3
undefinedgit tag -d v1.2.3
git push origin :refs/tags/v1.2.3
undefinedWorkflow Patterns
工作流模式
Git Flow
Git Flow
Branch structure:
- - Production releases
main - - Next release development
develop - - New features
feature/* - - Release preparation
release/* - - Emergency fixes
hotfix/*
Feature workflow:
bash
undefined分支结构:
- - 生产版本分支
main - - 下一个版本开发分支
develop - - 新功能分支
feature/* - - 版本发布准备分支
release/* - - 紧急修复分支
hotfix/*
功能分支工作流:
bash
undefinedStart feature
Start feature
git checkout develop
git pull origin develop
git checkout -b feature/123-new-feature
git checkout develop
git pull origin develop
git checkout -b feature/123-new-feature
Work on feature
Work on feature
git add .
git commit -m "feat: implement user authentication"
git add .
git commit -m "feat: implement user authentication"
Finish feature
Finish feature
git checkout develop
git pull origin develop
git merge --no-ff feature/123-new-feature
git push origin develop
git branch -d feature/123-new-feature
**Release workflow:**
```bashgit checkout develop
git pull origin develop
git merge --no-ff feature/123-new-feature
git push origin develop
git branch -d feature/123-new-feature
**版本发布工作流:**
```bashStart release
Start release
git checkout develop
git checkout -b release/v1.2.0
git checkout develop
git checkout -b release/v1.2.0
Prepare release (bump version, update changelog)
Prepare release (bump version, update changelog)
git commit -m "chore: prepare release v1.2.0"
git commit -m "chore: prepare release v1.2.0"
Merge to main
Merge to main
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release v1.2.0"
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release v1.2.0"
Merge back to develop
Merge back to develop
git checkout develop
git merge --no-ff release/v1.2.0
git checkout develop
git merge --no-ff release/v1.2.0
Cleanup
Cleanup
git branch -d release/v1.2.0
**Hotfix workflow:**
```bashgit branch -d release/v1.2.0
**紧急修复工作流:**
```bashStart hotfix from main
Start hotfix from main
git checkout main
git checkout -b hotfix/critical-bug
git checkout main
git checkout -b hotfix/critical-bug
Fix and commit
Fix and commit
git commit -m "fix: resolve critical security vulnerability"
git commit -m "fix: resolve critical security vulnerability"
Merge to main
Merge to main
git checkout main
git merge --no-ff hotfix/critical-bug
git tag -a v1.2.1 -m "Hotfix v1.2.1"
git checkout main
git merge --no-ff hotfix/critical-bug
git tag -a v1.2.1 -m "Hotfix v1.2.1"
Merge to develop
Merge to develop
git checkout develop
git merge --no-ff hotfix/critical-bug
git checkout develop
git merge --no-ff hotfix/critical-bug
Cleanup
Cleanup
git branch -d hotfix/critical-bug
undefinedgit branch -d hotfix/critical-bug
undefinedGitHub Flow
GitHub Flow
Simplified workflow:
- - Always deployable
main - - All changes in feature branches
feature/*
Workflow:
bash
undefined简化工作流:
- - 始终可部署
main - - 所有变更都在功能分支中进行
feature/*
工作流步骤:
bash
undefinedCreate feature branch
Create feature branch
git checkout -b feature/add-logging
git push -u origin feature/add-logging
git checkout -b feature/add-logging
git push -u origin feature/add-logging
Make changes and commit
Make changes and commit
git commit -m "feat: add structured logging"
git push origin feature/add-logging
git commit -m "feat: add structured logging"
git push origin feature/add-logging
Open pull request on GitHub
Open pull request on GitHub
After review and CI passes, merge to main
After review and CI passes, merge to main
Deploy from main
Deploy from main
undefinedundefinedTrunk-Based Development
主干开发模式
Single main branch:
- Short-lived feature branches (< 2 days)
- Frequent integration to main
- Feature flags for incomplete features
- Continuous integration
Workflow:
bash
undefined单一主分支:
- 短生命周期功能分支(<2天)
- 频繁合并到主分支
- 使用功能标记管理未完成功能
- 持续集成
工作流步骤:
bash
undefinedCreate short-lived branch
Create short-lived branch
git checkout -b update-api-docs
git push -u origin update-api-docs
git checkout -b update-api-docs
git push -u origin update-api-docs
Make small, incremental changes
Make small, incremental changes
git commit -m "docs: update API endpoint documentation"
git push origin update-api-docs
git commit -m "docs: update API endpoint documentation"
git push origin update-api-docs
Immediately create PR and merge (same day)
Immediately create PR and merge (same day)
Main branch always deployable with feature flags
Main branch always deployable with feature flags
undefinedundefinedPull Request Conventions
拉取请求约定
PR Title Format
PR标题格式
Use Conventional Commits format:
feat(auth): add OAuth2 provider support
fix(api): resolve rate limiting edge case
docs: update installation guide使用Conventional Commits格式:
feat(auth): add OAuth2 provider support
fix(api): resolve rate limiting edge case
docs: update installation guidePR Description Template
PR描述模板
markdown
undefinedmarkdown
undefinedSummary
Summary
Brief description of changes and motivation.
Brief description of changes and motivation.
Changes
Changes
- Bullet list of specific changes
- Reference architecture decisions
- Note any breaking changes
- Bullet list of specific changes
- Reference architecture decisions
- Note any breaking changes
Testing
Testing
- Unit tests added/updated
- Integration tests passed
- Manual testing performed
- Unit tests added/updated
- Integration tests passed
- Manual testing performed
Screenshots (if applicable)
Screenshots (if applicable)
[Add screenshots for UI changes]
[Add screenshots for UI changes]
Related Issues
Related Issues
Closes #123
Refs #456
Closes #123
Refs #456
Checklist
Checklist
- Tests added/updated
- Documentation updated
- Changelog updated
- Breaking changes documented
- Code reviewed by team
undefined- Tests added/updated
- Documentation updated
- Changelog updated
- Breaking changes documented
- Code reviewed by team
undefinedReview Guidelines
评审指南
Reviewer checklist:
- Code follows style guide
- Commit messages follow conventions
- Tests are comprehensive
- Documentation is updated
- No security vulnerabilities
- Performance considerations addressed
- Breaking changes are justified
评审者检查清单:
- 代码遵循风格指南
- 提交消息符合约定
- 测试用例全面
- 文档已更新
- 无安全漏洞
- 考虑了性能影响
- 破坏性变更已说明
Changelog Management
变更日志管理
Keep a Changelog Format
Keep a Changelog格式
markdown
undefinedmarkdown
undefinedChangelog
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog,
and this project adheres to Semantic Versioning.
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog,
and this project adheres to Semantic Versioning.
[Unreleased]
[Unreleased]
Added
Added
- User authentication with JWT tokens
- API rate limiting middleware
- User authentication with JWT tokens
- API rate limiting middleware
Changed
Changed
- Updated database schema for better performance
- Updated database schema for better performance
Deprecated
Deprecated
- Old authentication endpoint (use /api/v2/auth instead)
- Old authentication endpoint (use /api/v2/auth instead)
Removed
Removed
- Legacy XML API support
- Legacy XML API support
Fixed
Fixed
- Memory leak in cache implementation
- Race condition in concurrent requests
- Memory leak in cache implementation
- Race condition in concurrent requests
Security
Security
- Patch for SQL injection vulnerability
- Patch for SQL injection vulnerability
[1.2.0] - 2024-03-15
[1.2.0] - 2024-03-15
Added
Added
- Real-time notifications system
- User profile customization
- Real-time notifications system
- User profile customization
Fixed
Fixed
- Login redirect loop issue
- Session timeout handling
- Login redirect loop issue
- Session timeout handling
[1.1.0] - 2024-02-01
[1.1.0] - 2024-02-01
Added
Added
- Search functionality
- Export to CSV feature
- Search functionality
- Export to CSV feature
Changed
Changed
- Improved UI responsiveness
undefined- Improved UI responsiveness
undefinedAutomated Changelog
自动化变更日志
Use tools like:
- - Generate changelog from commits
conventional-changelog - - Automated releases and changelog
release-please - - Fully automated version management
semantic-release
可使用以下工具:
- - 从提交记录生成变更日志
conventional-changelog - - 自动化版本发布与变更日志生成
release-please - - 全自动化版本管理
semantic-release
Best Practices
最佳实践
- Commit Often: Small, focused commits are easier to review and revert
- Write Clear Messages: Future you will thank present you
- One Concern Per Commit: Each commit should address one logical change
- Test Before Committing: Ensure code works before committing
- Reference Issues: Link commits to issue tracker
- Review Your Own Changes: Use before committing
git diff --staged - Keep History Clean: Rebase feature branches to keep linear history
- Sign Your Commits: Use GPG signing for verified commits
- Use .gitignore Properly: Never commit sensitive or generated files
- Document Conventions: Keep team conventions in repository docs
- 频繁提交: 小型、聚焦的提交更易于评审和回滚
- 撰写清晰的消息: 未来的你会感谢现在的自己
- 每次提交单一关注点: 每个提交应解决一个逻辑变更
- 提交前测试: 确保代码可正常运行后再提交
- 关联议题: 将提交与议题追踪系统关联
- 自我评审变更: 提交前使用检查变更
git diff --staged - 保持历史清晰: 对功能分支进行变基以维持线性历史
- 签署提交: 使用GPG签名以验证提交
- 正确使用.gitignore: 绝不提交敏感文件或生成文件
- 记录约定: 将团队约定保存在仓库文档中
Team Workflow Examples
团队工作流示例
Small Team (2-5 developers)
小型团队(2-5人)
bash
undefinedbash
undefinedSimplified workflow
Simplified workflow
- Direct commits to main (with PR reviews)
- Feature branches for major changes
- Tags for releases
- Linear history preferred
undefined- Direct commits to main (with PR reviews)
- Feature branches for major changes
- Tags for releases
- Linear history preferred
undefinedMedium Team (5-20 developers)
中型团队(5-20人)
bash
undefinedbash
undefinedGit Flow variant
Git Flow variant
- Protected main and develop branches
- Feature branches required
- Release branches for versions
- Hotfix workflow for emergencies
- Squash merge for clean history
undefined- Protected main and develop branches
- Feature branches required
- Release branches for versions
- Hotfix workflow for emergencies
- Squash merge for clean history
undefinedLarge Team (20+ developers)
大型团队(20+人)
bash
undefinedbash
undefinedTrunk-based with feature flags
Trunk-based with feature flags
- Protected main branch
- Very short-lived feature branches
- Feature flags for incomplete work
- Automated testing and deployment
- Multiple daily integrations
undefined- Protected main branch
- Very short-lived feature branches
- Feature flags for incomplete work
- Automated testing and deployment
- Multiple daily integrations
undefinedResources
资源
Additional guides and templates are available in the directory:
assets/- - Commit message and PR templates
templates/ - - Real-world workflow examples
examples/ - - Git hooks and automation scripts
tools/
See directory for:
references/- Conventional Commits specification
- Semantic Versioning documentation
- Git Flow and GitHub Flow guides
- Keep a Changelog standards
更多指南和模板可在目录中获取:
assets/- - 提交消息和PR模板
templates/ - - 真实工作流示例
examples/ - - Git钩子和自动化脚本
tools/
references/- Conventional Commits规范
- 语义化版本控制文档
- Git Flow与GitHub Flow指南
- Keep a Changelog标准