Skip to content

Git Flow Workflow

Git Flow branching model — feature, 发布, and hotfix 分支 for structured 发布.

你将学到

  • Git Flow branching model and 它的分支类型
  • 如何 feature, 发布, and hotfix 分支工作
  • 当 Git Flow 是 appropriate 对比 simpler workflows
  • git-flow extension commands

概念讲解

What Is Git Flow?

Git Flow is a branching model proposed by Vincent Driessen in 2010. It defines a strict set of branch types and rules for how they interact, designed for projects with scheduled releases (like versioned software products).

The Branch Types

Git Flow uses five types of branches:

  1. 主要 — production-ready code. Every commit on main is a release. Tags mark versions.
  2. develop — the integration branch for features. Contains the latest delivered features for the next release.
  3. **feature/*** — branched from develop, merged back to develop. Each new feature gets its own branch.
  4. **发布/*** — branched from develop, merged to both 主要 and develop. Prepares a release (version bumps, last fixes).
  5. **hotfix/*** — branched from 主要, merged to both 主要 and develop. Fixes production bugs urgently.

The Workflow

main:    A-------------------M---H---  (tags: v1.0, v1.1)
              \             / \ /
develop:       B---F---G---R---/
                    \   / 
feature/x:          C---D---F  (merged to develop)
  1. Start a feature: git checkout -b feature/登录 develop
  2. Finish a feature: merge into develop
  3. Start a release: git checkout -b 发布/1.1 develop
  4. Finish a release: merge into 主要 and develop, tag on 主要
  5. Hotfix: git checkout -b hotfix/bug 主要, fix, merge into 主要 and develop

The git-flow Extension

The git-flow extension provides shortcut commands:

git flow init                    # initialize Git Flow
git flow feature start login     # create feature/login from develop
git flow feature finish login    # merge feature into develop
git flow release start 1.1       # create release/1.1
git flow release finish 1.1      # merge to main + develop, tag
git flow hotfix start bug        # create hotfix/bug from main
git flow hotfix finish bug       # merge to main + develop, tag

Install it with brew 安装 git-flow (macOS) or your package manager.

Git Flow vs GitHub Flow

Git Flow is powerful but complex. For many projects, a simpler model works better:

GitHub Flow (simpler):

  • One 主要 branch
  • Feature branches off 主要
  • Pull requests to merge into 主要
  • Deploy from 主要 continuously

Trunk-Based Development (simplest):

  • Everyone commits to 主要 (or short-lived branches)
  • Feature flags for incomplete work
  • Continuous deployment

When to Use Git Flow

Git Flow is a good fit for:

  • Versioned products with scheduled releases (e.g., desktop apps, libraries)
  • Multiple maintained versions (you need to hotfix old releases)
  • Teams that need structure and clear release phases

It is overkill for:

  • Web apps with continuous deployment
  • Small teams that can communicate informally
  • Projects where releases happen multiple times per day

Choose the simplest workflow that works for your team. You can always adopt Git Flow later if you need more structure.

示例代码

              # --- Initialize Git Flow (creates develop branch, etc.) ---
git flow init

# --- Feature branch workflow ---
git flow feature start user-auth
# ... work on the feature ...
git add .
git commit -m "feat: add user authentication"
git flow feature finish user-auth
# merges feature/user-auth into develop, deletes the feature branch

# --- Release workflow ---
git flow release start 1.2.0
# bump version numbers, last-minute fixes
git commit -am "chore: bump version to 1.2.0"
git flow release finish 1.2.0
# merges release into main AND develop, tags v1.2.0

# --- Hotfix workflow (urgent production bug) ---
git flow hotfix start critical-security-bug
# fix the bug
git commit -am "fix: patch security vulnerability"
git flow hotfix finish critical-security-bug
# merges into main AND develop, tags the hotfix

# --- Without the extension (manual Git Flow) ---
git checkout develop
git checkout -b feature/new-api
# ... work ...
git checkout develop
git merge --no-ff feature/new-api
git branch -d feature/new-api

# Release branch manually
git checkout develop
git checkout -b release/2.0.0
# ... version bump, fixes ...
git checkout main
git merge --no-ff release/2.0.0
git tag -a v2.0.0 -m "Release 2.0.0"
git checkout develop
git merge --no-ff release/2.0.0
git branch -d release/2.0.0
            

Shows git-flow extension commands for features, 发布, and hotfixes, plus equivalent manual Git commands. 键 insight 是那个发布合并到 两者都主要 and develop, and hotfixes 做相同. 这个 ensures 主要 and develop stay in sync.

动手试一试

常见错误

错误写法

Using Git Flow for continuously deployed web app

正确做法

Git Flow's 发布分支添加 overhead 那个做 不 pay off for web apps deployed 多个次数每 day. For continuous 部署, use GitHub Flow (feature 分支 + PRs 到主要) or Trunk-Based Development. Reserve Git Flow for versioned products 使用 scheduled 发布.

错误写法

Forgetting 到合并发布/hotfix 分支后面到 两者都主要 and develop

正确做法

In Git Flow, 发布 and hotfix 分支必须是 合并到 BOTH 主要 and develop. 如果你 仅合并到 主要, develop 将是 missing 更改 and 它们将 不是 in 下一个发布. git-flow extension handles 这个 automatically; 如果做 它 manually, 总是合并到 两者都.

相关速查表

练习 Git Flow Workflow

2 个练习

练习 Git Flow Workflow

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误