git-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Workflow
Git工作流
Effective version control is not just about tracking changes -- it is about enabling teams to collaborate predictably, release confidently, and maintain a history that tells a coherent story. The right workflow depends on team size, release cadence, and how much process the project can absorb without slowing down.
高效的版本控制不仅仅是追踪变更——它还能帮助团队实现可预测的协作、可靠的发布,并且维护一段逻辑连贯的变更历史。合适的工作流取决于团队规模、发布节奏,以及项目可承受且不会拖慢效率的流程复杂度。
Choosing a Branching Strategy
选择分支策略
No single branching model fits every team. The right choice depends on how often you release, how large your team is, and how much ceremony you can tolerate.
没有任何一种分支模型能适配所有团队。合适的选择取决于你的发布频率、团队规模,以及可接受的流程繁琐程度。
Strategy Comparison
策略对比
| Strategy | Release Cadence | Team Size | Complexity | Best For |
|---|---|---|---|---|
| Trunk-Based | Continuous (multiple per day) | Any (works best with strong CI) | Low | SaaS, cloud-native, teams with mature CI/CD |
| GitHub Flow | On-demand (per merged PR) | Small to medium | Low | Web apps, startups, teams wanting simplicity |
| Git Flow | Scheduled / versioned releases | Medium to large | High | Packaged software, mobile apps, multiple supported versions |
| GitLab Flow | Environment-promoted | Medium to large | Medium | Teams needing environment-specific branches (staging, production) |
| 策略 | 发布节奏 | 团队规模 | 复杂度 | 适用场景 |
|---|---|---|---|---|
| Trunk-Based | 持续发布(每天多次) | 任意规模(搭配成熟CI效果最佳) | 低 | SaaS、云原生项目、拥有成熟CI/CD的团队 |
| GitHub Flow | 按需发布(PR合并即发布) | 中小规模 | 低 | Web应用、初创团队、追求简洁流程的团队 |
| Git Flow | 定期/按版本发布 | 中大规模 | 高 | 打包软件、移动应用、需要维护多个支持版本的项目 |
| GitLab Flow | 按环境递进发布 | 中大规模 | 中等 | 需要区分环境分支(预发、生产)的团队 |
Decision Guide
决策指南
- Do you deploy continuously? -- Use trunk-based development. Short-lived branches (hours, not days), feature flags for incomplete work, and strong CI are prerequisites.
- Do you deploy on merge but want review gates? -- Use GitHub Flow. One long-lived branch (main), feature branches, pull requests as the merge gate.
- Do you ship versioned releases on a schedule? -- Use Git Flow. Separate develop and main branches, release branches for stabilization, hotfix branches for emergency patches.
- Do you promote through environments? -- Use GitLab Flow. Environment branches (staging, production) sit downstream of main, and merges flow in one direction.
See Branching Strategies Reference for detailed mechanics, feature flag integration, and migration paths between strategies.
- 你是否需要持续部署?——使用基于主干的开发模式。前置要求为短生命周期分支(存续数小时而非数天)、未完工功能使用特性开关隐藏、拥有成熟的CI体系。
- 你是否在合并时部署,但需要评审关卡?——使用GitHub Flow。仅保留一个长期分支(main)、使用功能分支、以拉取请求作为合并准入门槛。
- 你是否按计划发布带版本号的版本?——使用Git Flow。拆分develop和main分支,用发布分支做版本固化,用热修复分支处理紧急补丁。
- 你是否需要跨环境递进发布?——使用GitLab Flow。环境分支(预发、生产)位于main分支下游,合并仅单向流动。
查看分支策略参考了解详细机制、特性开关集成以及不同策略之间的迁移路径。
Commit Message Conventions
提交信息规范
Good commit messages serve three audiences: reviewers reading the PR, developers reading next month, and automation tools generating changelogs and version bumps.
git log优质的提交信息服务于三类受众:阅读PR的评审者、下个月查看的开发者、生成变更日志和版本号的自动化工具。
git logThe Conventional Commits Format
约定式提交(Conventional Commits)格式
<type>(<scope>): <description>
[optional body]
[optional footer(s)]<type>(<scope>): <description>
[optional body]
[optional footer(s)]Common Commit Types
常用提交类型
| Type | Purpose | Version Impact |
|---|---|---|
| New feature visible to users | Minor bump |
| Bug fix | Patch bump |
| Code restructuring with no behavior change | None |
| Performance improvement | Patch bump |
| Adding or updating tests | None |
| Documentation only | None |
| Tooling, dependencies, build config | None |
| CI/CD pipeline changes | None |
| Build system or dependency changes | None |
| 类型 | 用途 | 版本影响 |
|---|---|---|
| 用户可见的新功能 | 次版本号升级 |
| Bug修复 | 修订版本号升级 |
| 不改变行为的代码重构 | 无影响 |
| 性能优化 | 修订版本号升级 |
| 新增或更新测试用例 | 无影响 |
| 仅文档变更 | 无影响 |
| 工具链、依赖、构建配置变更 | 无影响 |
| CI/CD流水线变更 | 无影响 |
| 构建系统或依赖变更 | 无影响 |
Breaking Changes
破坏性变更
Append after the type or add a footer to signal incompatible changes. Either notation triggers a major version bump when used with automated release tooling.
!BREAKING CHANGE:feat(api)!: remove deprecated /v1/users endpoint
BREAKING CHANGE: The /v1/users endpoint has been removed. Use /v2/users instead.在类型后追加或者添加页脚来标记不兼容变更。当搭配自动化发布工具使用时,任意一种标记都会触发主版本号升级。
!BREAKING CHANGE:feat(api)!: remove deprecated /v1/users endpoint
BREAKING CHANGE: The /v1/users endpoint has been removed. Use /v2/users instead.Semantic Versioning Alignment
语义化版本对齐
| Commit Contains | Version Bump | Example |
|---|---|---|
| Patch (1.0.0 -> 1.0.1) | Bug corrections |
| Minor (1.0.0 -> 1.1.0) | New capabilities |
| Major (1.0.0 -> 2.0.0) | Incompatible changes |
See Commit Conventions Reference for scope naming patterns, multi-line body guidelines, changelog automation setup, and git hook validation.
| 提交包含内容 | 版本号升级 | 示例 |
|---|---|---|
| 修订号(1.0.0 -> 1.0.1) | Bug修复 |
| 次版本号(1.0.0 -> 1.1.0) | 新增功能 |
| 主版本号(1.0.0 -> 2.0.0) | 不兼容变更 |
查看提交规范参考了解范围命名模式、多行正文规范、变更日志自动化配置以及Git钩子校验方法。
Pull Request Workflow
拉取请求(PR)工作流
Pull requests are the primary collaboration point in most git workflows. Their quality directly affects review speed, defect discovery, and team velocity.
拉取请求是大多数Git工作流中的核心协作节点。它的质量直接影响评审速度、缺陷发现效率和团队迭代速度。
PR Size Guidelines
PR规模指引
| PR Size (lines changed) | Review Quality | Recommended |
|---|---|---|
| < 50 | Excellent -- reviewer catches most issues | Ideal for stacked PRs |
| 50-200 | Good -- manageable cognitive load | Sweet spot for most changes |
| 200-400 | Acceptable -- requires focused review time | Upper bound for single reviews |
| 400+ | Poor -- reviewer fatigue, defects slip through | Split into smaller PRs |
| PR规模(变更行数) | 评审质量 | 推荐程度 |
|---|---|---|
| < 50 | 优秀——评审者能发现绝大多数问题 | 堆叠PR的理想选择 |
| 50-200 | 良好——认知负荷可控 | 绝大多数变更的最优区间 |
| 200-400 | 可接受——需要集中评审时间 | 单次评审的上限 |
| 400+ | 较差——评审者疲劳,缺陷容易遗漏 | 拆分为更小的PR |
Core PR Practices
核心PR实践
- One concern per PR -- A PR should do one thing: add a feature, fix a bug, refactor a module. Mixing concerns makes review harder and reverts riskier.
- Write a clear description -- State what changed, why it changed, and how to verify it. Link to the relevant ticket or issue.
- Keep the diff reviewable -- Move large mechanical changes (renames, formatting) into separate PRs from logic changes.
- Respond to feedback promptly -- Stale PRs accumulate merge conflicts and block dependent work.
- 每个PR仅解决一个问题——一个PR应该只做一件事:新增功能、修复Bug、重构某个模块。混合多个变更会提升评审难度,也会提高回滚风险。
- 编写清晰的描述——说明变更内容、变更原因以及验证方法,关联对应的工单或Issue。
- 保持diff可评审——将大量机械变更(重命名、格式化)和逻辑变更拆分到不同的PR中。
- 及时响应评审反馈——搁置的PR容易产生合并冲突,也会阻塞依赖它的后续工作。
Merge Strategies
合并策略
| Strategy | History Shape | Best For |
|---|---|---|
| Merge commit | Preserves branch topology, creates merge node | Open-source projects, audit trails |
| Squash and merge | Collapses branch into single commit on main | Feature branches with messy intermediate commits |
| Rebase and merge | Linear history, no merge commits | Teams wanting clean, linear logs |
See PR Patterns Reference for description templates, CODEOWNERS configuration, branch protection rules, stacked PR workflows, and review assignment strategies.
| 策略 | 历史结构 | 适用场景 |
|---|---|---|
| 合并提交(Merge commit) | 保留分支拓扑,生成合并节点 | 开源项目、需要审计轨迹的场景 |
| 压缩合并(Squash and merge) | 将分支所有提交压缩为main分支上的单个提交 | 中间提交混乱的功能分支 |
| 变基合并(Rebase and merge) | 线性历史,无合并提交 | 追求整洁线性提交日志的团队 |
查看PR模式参考了解描述模板、CODEOWNERS配置、分支保护规则、堆叠PR工作流以及评审分配策略。
Release Management
发布管理
Release management bridges development and deployment. The approach depends on whether releases are continuous, scheduled, or versioned.
发布管理是开发和部署之间的桥梁。具体方案取决于发布是持续式、定期式还是按版本发布。
Release Models
发布模型
| Model | How It Works | When to Use |
|---|---|---|
| Continuous release | Every merged PR deploys automatically | SaaS with strong CI/CD and monitoring |
| Scheduled release | Changes accumulate, release on a cadence (weekly, biweekly) | Teams needing coordination windows |
| Versioned release | Explicit version tags, release branches for stabilization | Libraries, APIs, packaged software |
| 模型 | 工作方式 | 适用场景 |
|---|---|---|
| 持续发布 | 每个合并的PR自动部署 | 拥有成熟CI/CD和监控体系的SaaS产品 |
| 定期发布 | 变更累积后按固定节奏发布(每周、每两周) | 需要协调发布窗口的团队 |
| 按版本发布 | 显式标记版本号,用发布分支做版本固化 | 类库、API、打包软件 |
Git Tags for Releases
用于发布的Git标签
Tags mark specific commits as release points. Use annotated tags for releases because they store author, date, and message metadata.
bash
undefined标签将特定提交标记为发布节点。推荐使用附注标签做发布标记,因为它们会存储作者、日期和消息元数据。
bash
undefinedCreate an annotated release tag
创建一个附注发布标签
git tag -a v1.2.0 -m "Release 1.2.0: Add user dashboard and fix auth timeout"
git tag -a v1.2.0 -m "Release 1.2.0: Add user dashboard and fix auth timeout"
Push tags to remote
推送标签到远程仓库
git push origin v1.2.0
git push origin v1.2.0
List existing tags matching a pattern
列出匹配指定模式的现有标签
git tag -l "v1.*"
undefinedgit tag -l "v1.*"
undefinedAutomated Release Pipeline
自动化发布流水线
When conventional commits are combined with release automation tooling, the pipeline can:
- Analyze commits since the last tag to determine the next version
- Generate or update the changelog from commit messages
- Create the git tag
- Build and publish artifacts
- Create a release entry on the hosting platform (GitHub/GitLab)
This eliminates manual version decisions and ensures the changelog stays synchronized with actual changes.
当约定式提交搭配发布自动化工具使用时,流水线可以:
- 分析上一个标签以来的所有提交,确定下一个版本号
- 根据提交信息生成或更新变更日志
- 创建Git标签
- 构建并发布制品
- 在代码托管平台(GitHub/GitLab)上创建发布条目
这可以消除手动版本决策的成本,确保变更日志和实际变更保持同步。
Hotfix Workflow
热修复工作流
When a critical bug is found in production:
- Branch from the release tag (not from the development branch)
- Apply the minimal fix
- Tag the new patch release
- Merge the fix forward into the main development line to prevent regression
bash
undefined当生产环境发现严重Bug时:
- 从发布标签切出分支(不要从开发分支切)
- 提交最小化修复方案
- 标记新的补丁版本标签
- 将修复向前合并到主开发分支,避免后续版本出现回归
bash
undefinedBranch from the release tag
从发布标签切出分支
git checkout -b hotfix/fix-auth-crash v1.2.0
git checkout -b hotfix/fix-auth-crash v1.2.0
After fixing and committing
修复并提交后
git tag -a v1.2.1 -m "Hotfix: prevent auth crash on expired tokens"
git push origin v1.2.1
git tag -a v1.2.1 -m "Hotfix: prevent auth crash on expired tokens"
git push origin v1.2.1
Merge fix back to main
将修复合并回main分支
git checkout main
git merge hotfix/fix-auth-crash
---git checkout main
git merge hotfix/fix-auth-crash
---Quick Reference: Common Workflow Problems
快速参考:常见工作流问题
| Problem | Likely Cause | Resolution |
|---|---|---|
| Frequent merge conflicts | Long-lived branches, infrequent integration | Merge main into feature branches daily, or switch to trunk-based |
| Broken main branch | No CI on PRs, insufficient test coverage | Require passing CI before merge, add branch protection |
| Unclear release contents | No commit conventions, manual changelogs | Adopt conventional commits, automate changelog generation |
| Slow PR reviews | PRs too large, no clear ownership | Set size guidelines, configure CODEOWNERS, use stacked PRs |
| Accidental commits to main | No branch protection | Enable branch protection rules, require PR reviews |
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 频繁出现合并冲突 | 分支生命周期长、集成频率低 | 每天将main分支合并到功能分支,或者切换到基于主干的开发模式 |
| main分支被破坏 | PR没有经过CI校验、测试覆盖不足 | 要求合并前CI必须通过,添加分支保护规则 |
| 发布内容不清晰 | 没有提交规范、手动维护变更日志 | 采用约定式提交,自动化生成变更日志 |
| PR评审速度慢 | PR过大、没有明确责任人 | 设定PR规模指引、配置CODEOWNERS、使用堆叠PR |
| 意外提交到main分支 | 没有分支保护 | 开启分支保护规则,要求必须经过PR评审才能合并 |
Reference Files
参考文件
| Reference | Contents |
|---|---|
| Branching Strategies | Trunk-based, GitHub Flow, Git Flow, GitLab Flow -- mechanics, feature flags, comparison matrix |
| Commit Conventions | Conventional commits format, scope patterns, semantic versioning, changelog automation, git hooks |
| PR Patterns | Size guidelines, description templates, CODEOWNERS, merge strategies, branch protection, stacked PRs |
| 参考文档 | 内容 |
|---|---|
| 分支策略 | 基于主干开发、GitHub Flow、Git Flow、GitLab Flow——实现机制、特性开关、对比矩阵 |
| 提交规范 | 约定式提交格式、范围模式、语义化版本、变更日志自动化、Git钩子 |
| PR模式 | 规模指引、描述模板、CODEOWNERS、合并策略、分支保护、堆叠PR |
Integration with Other Skills
与其他技能的集成
| Situation | Recommended Skill |
|---|---|
| Setting up CI/CD pipelines for branch protection | Install |
| API versioning aligned with git release tags | Install |
| Coordinating releases across microservices | Install |
| Sprint-aligned release planning | Install |
| 场景 | 推荐技能 |
|---|---|
| 配置用于分支保护的CI/CD流水线 | 从 |
| 与Git发布标签对齐的API版本控制 | 从 |
| 跨微服务的发布协调 | 从 |
| 与Sprint对齐的发布规划 | 从 |