Git Branching Strategies

部分內容由 LLM 生成,尚未經過人工驗證。

Git Flow

特點:

  • main(production)
  • develop
  • feature
  • release
  • hotfix
  gitGraph
commit id:"init"

branch develop
commit id:"develop start"

branch feature-login
commit id:"login feature"
commit id:"login finish"

checkout develop
merge feature-login

branch release-1.0
commit id:"release fix"

checkout main
merge release-1.0
commit id:"prod release"

checkout develop
merge release-1.0

Flow 邏輯: feature → develop → release → main


GitHub Flow

特點:

  • 只有 main
  • feature → PR → deploy
  gitGraph
commit id:"init"

branch feature-login
commit id:"develop login"
commit id:"finish login"

checkout main
merge feature-login
commit id:"deploy"

Flow 邏輯: feature → main → deploy


GitLab Flow

(Environment Branch)

常見結構: main → staging → production

  gitGraph
   commit id: "init"
   branch feature-login
   commit id: "develop login"
   commit id: "finish login"
   checkout main
   merge feature-login
   branch staging
   commit id: "QA test"
   branch production
   commit id: "deploy"

Flow 邏輯:

feature → main → staging → production

公司內部 Flow

(最接近 GitLab Flow)

描述:

  • feature 從 prod tag checkout
  • feature 可 merge dev / beta
  • sprint 挑 ticket merge prod
  • prod 打 tag release
  gitGraph
commit id:"v1.0"

branch prod
branch dev
branch beta

checkout prod
branch feature-JIRA123
commit id:"feature dev"
commit id:"feature done"

checkout dev
merge feature-JIRA123
commit id:"dev test"

checkout beta
merge feature-JIRA123
commit id:"qa test"

checkout prod
merge feature-JIRA123
commit id:"release v1.1"

Flow 邏輯:

prod(tag) → feature/JIRA → dev → beta → prod release

企業級 Flow

Trunk-based + Feature Flags + Release Gates

此策略接近 Google / Stripe / Uber 大型團隊實務

Flow Overview

feature → dev → staging → preprod → release/x.y → main (prod + tag)

Branch 職責

Branch職責
main永遠代表 Production 可 deploy 版本
dev每天 CI/CD 測試,短期 feature 合併
stagingQA 整合測試,確保多 feature 同時整合不衝突
preprodUAT / 客戶驗收 / release freeze
release/x.yRelease Candidate,最終測試與審核
feature/*短期分支,搭配 Feature Flags 控制開關

實務特點

  • 短期 feature branch — 每天合併回 dev,避免長期分支造成 merge 地獄
  • Feature Flags — 功能可提前合併但保持關閉,main 隨時可 deploy
  • 環境 Promotion — 每個環境 merge 都保留 commit,維持完整歷史
  • Release Gates — 每個 promotion 都有對應的測試 / QA / UAT gate

相較傳統 Git Flow 的優勢

  • 更貼近 CI/CD 節奏
  • 支援多團隊平行開發
  • 降低 long-lived branch 的 merge 風險
  • Feature Flag 讓 deploy 與 release 解耦
  gitGraph
   commit id: "init commit"
   commit id: "prod v1.0"

   branch dev
   branch staging
   branch preprod
   branch release/2.0

   checkout dev
   commit id: "sync from main"

   branch feature/A
   commit id: "feat A start"
   commit id: "feat A done"
   checkout dev
   merge feature/A
   commit id: "dev after A"

   branch feature/B
   commit id: "feat B start"
   commit id: "feat B done"
   checkout dev
   merge feature/B
   commit id: "dev after B"

   branch feature/C
   commit id: "feat C (flag off)"
   commit id: "feat C done"
   checkout dev
   merge feature/C
   commit id: "dev after C (flag off)"

   checkout staging
   merge dev
   commit id: "staging tests"

   checkout preprod
   merge staging
   commit id: "preprod validation"

   checkout release/2.0
   merge preprod
   commit id: "RC v2.0"

   checkout main
   merge release/2.0
   commit id: "prod v2.0"

各策略總覽對照

策略複雜度適用團隊規模CI/CD 友善度特色
Git Flow中大型普通嚴格分支職責,適合定期 release
GitHub Flow小型簡單,適合快速迭代
GitLab Flow中型環境對應分支,部署彈性
公司內部中型從 prod tag 出發,sprint 節奏控制
企業級 Trunk-based大型最高Feature Flags + Release Gates,多環境 promotion