Trunk-Based Development vs Git Flow: Chọn đúng Chiến lược Branching cho Team 2026
Posted on: 4/21/2026 1:15:07 PM
Table of contents
- Git Flow là gì?
- Trunk-Based Development là gì?
- GitHub Flow và GitLab Flow
- So sánh chi tiết: Git Flow vs Trunk-Based Development
- Feature Flags — Chìa khóa cho Trunk-Based Development
- DORA Metrics và Branching Strategy
- Khi nào chọn Git Flow, khi nào chọn TBD?
- Chuyển đổi từ Git Flow sang Trunk-Based Development
- Best Practices cho Trunk-Based Development
- Thực tế tại các công ty lớn
- Tổng kết
Chiến lược branching quyết định tốc độ ship code, chất lượng review, và khả năng rollback của team. Chọn sai mô hình Git có thể biến CI/CD pipeline thành nơi merge conflict chồng chất, release bị block hàng tuần, và developer mất thời gian resolve xung đột thay vì viết feature mới.
Bài viết này phân tích sâu hai mô hình phổ biến nhất — Git Flow và Trunk-Based Development (TBD) — kèm theo GitHub Flow, GitLab Flow, và dữ liệu từ DORA Research 2025–2026 để giúp bạn đưa ra quyết định phù hợp cho team mình.
Git Flow là gì?
Git Flow được Vincent Driessen giới thiệu năm 2010, nhanh chóng trở thành mô hình branching phổ biến nhất. Nó định nghĩa rõ ràng vai trò của từng nhánh trong vòng đời phát triển phần mềm.
gitGraph
commit id: "init"
branch develop
checkout develop
commit id: "setup"
branch feature/login
checkout feature/login
commit id: "login-ui"
commit id: "login-api"
checkout develop
merge feature/login id: "merge-login"
branch release/1.0
checkout release/1.0
commit id: "bump-version"
commit id: "fix-typo"
checkout main
merge release/1.0 id: "v1.0" tag: "v1.0"
checkout develop
merge release/1.0 id: "sync-develop"
checkout main
branch hotfix/1.0.1
commit id: "critical-fix"
checkout main
merge hotfix/1.0.1 id: "v1.0.1" tag: "v1.0.1"
checkout develop
merge hotfix/1.0.1 id: "sync-hotfix"
Hình 1: Mô hình Git Flow với 5 loại nhánh — main, develop, feature/*, release/*, hotfix/*
Cấu trúc nhánh trong Git Flow
| Nhánh | Vai trò | Lifetime |
|---|---|---|
main | Production code, mỗi commit là một release | Vĩnh viễn |
develop | Integration branch, nơi merge tất cả feature | Vĩnh viễn |
feature/* | Phát triển tính năng mới | Ngày → tuần |
release/* | Chuẩn bị release, fix bug cuối | Vài ngày |
hotfix/* | Sửa lỗi khẩn cấp trên production | Vài giờ → 1 ngày |
Ưu điểm của Git Flow
- Cấu trúc rõ ràng: Mỗi nhánh có mục đích cụ thể, dễ hiểu cho team mới.
- Parallel development: Nhiều feature phát triển đồng thời không ảnh hưởng nhau.
- Release kiểm soát: Quy trình release có bước chuẩn bị riêng, phù hợp với scheduled releases (mobile app, enterprise software).
- Hotfix tách biệt: Fix production không cần chờ feature đang phát triển dở.
Nhược điểm của Git Flow
- Merge hell: Feature branch sống lâu (tuần → tháng) tích lũy divergence lớn, merge về develop trở thành ác mộng.
- Feedback loop chậm: Code review xảy ra khi feature "xong hết" → PR khổng lồ, reviewer overwhelmed.
- Release bottleneck: Release branch tạo thêm ceremony — version bump, cherry-pick fix, merge ngược về develop — dễ bỏ sót.
- Không phù hợp CI/CD: Triết lý "deploy bất cứ lúc nào" mâu thuẫn với quy trình release branch kéo dài.
⚠ Lời thú nhận từ chính tác giả Git Flow
Vincent Driessen, người tạo ra Git Flow, đã thêm ghi chú vào bài viết gốc năm 2020: "If your team is doing continuous delivery of software, I would suggest to adopt a much simpler workflow (like GitHub flow) instead of trying to shoehorn git-flow into your team." — Chính tác giả cũng khuyên không nên dùng Git Flow cho continuous delivery.
Trunk-Based Development là gì?
Trunk-Based Development (TBD) là mô hình trong đó tất cả developer commit trực tiếp vào một nhánh chính (trunk/main), hoặc sử dụng short-lived feature branches (tồn tại tối đa 1–2 ngày) rồi merge ngay. Không có nhánh develop, không có release branch kéo dài.
gitGraph
commit id: "feature-a"
branch feat/search
checkout feat/search
commit id: "search-v1"
checkout main
merge feat/search id: "merge-search"
commit id: "feature-b"
commit id: "feature-c"
branch feat/filter
checkout feat/filter
commit id: "filter-ui"
checkout main
merge feat/filter id: "merge-filter" tag: "auto-deploy"
commit id: "feature-d"
commit id: "feature-e" tag: "auto-deploy"
Hình 2: Trunk-Based Development — short-lived branches merge nhanh, main luôn deployable
Hai biến thể TBD
flowchart LR
subgraph A["Commit trực tiếp vào Trunk"]
A1["Developer A commit"] --> T1["main"]
A2["Developer B commit"] --> T1
A3["Developer C commit"] --> T1
end
subgraph B["Short-lived Feature Branches"]
B1["Developer A"] --> FB1["feat/x
(sống < 2 ngày)"]
FB1 --> T2["main"]
B2["Developer B"] --> FB2["feat/y
(sống < 1 ngày)"]
FB2 --> T2
end
style T1 fill:#e94560,stroke:#fff,color:#fff
style T2 fill:#e94560,stroke:#fff,color:#fff
style FB1 fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style FB2 fill:#f8f9fa,stroke:#e94560,color:#2c3e50
Hình 3: Hai biến thể — commit thẳng trunk (team nhỏ) vs short-lived branches (team lớn hơn, cần code review)
Ưu điểm của TBD
- Merge conflict tối thiểu: Branch sống ngắn = ít divergence = ít xung đột.
- CI/CD tự nhiên: Main luôn ở trạng thái deployable, pipeline chạy liên tục trên mỗi commit.
- Code review nhanh: PR nhỏ (vài chục → vài trăm dòng) review trong 15–30 phút thay vì hàng giờ.
- Feedback loop nhanh: Developer biết code có lỗi trong vài phút thay vì cuối sprint.
- DORA-proven: Nghiên cứu DORA chỉ ra TBD là đặc điểm chung của Elite performing teams.
Nhược điểm của TBD
- Yêu cầu automated testing mạnh: Không có test tốt, code lỗi sẽ ảnh hưởng toàn team ngay lập tức.
- Feature flags cần thiết: Feature chưa hoàn thành phải ẩn sau flag, tăng complexity quản lý.
- Đòi hỏi discipline: Developer phải commit thường xuyên, chia nhỏ task, không "ôm code" nhiều ngày.
- Khó áp dụng cho team junior nhiều: Cần hiểu biết về CI/CD, testing, và feature flag.
GitHub Flow và GitLab Flow
Ngoài Git Flow và TBD, có hai mô hình trung gian phổ biến:
flowchart TB
subgraph GHF["GitHub Flow"]
direction LR
M1["main
(luôn deployable)"]
F1["feature branch"] -->|PR + Review| M1
M1 -->|Deploy ngay| P1["Production"]
end
subgraph GLF["GitLab Flow"]
direction LR
M2["main"]
F2["feature branch"] -->|MR + Review| M2
M2 --> E1["pre-production"]
E1 --> E2["production"]
end
style M1 fill:#e94560,stroke:#fff,color:#fff
style M2 fill:#e94560,stroke:#fff,color:#fff
style P1 fill:#4CAF50,stroke:#fff,color:#fff
style E2 fill:#4CAF50,stroke:#fff,color:#fff
style E1 fill:#ff9800,stroke:#fff,color:#fff
Hình 4: GitHub Flow (đơn giản nhất) vs GitLab Flow (thêm environment branches)
| Mô hình | Số loại nhánh | Độ phức tạp | Phù hợp |
|---|---|---|---|
| Git Flow | 5 (main, develop, feature, release, hotfix) | Cao | Scheduled releases, mobile app, versioned software |
| GitHub Flow | 2 (main, feature) | Thấp | Web app, SaaS, continuous deployment |
| GitLab Flow | 3–4 (main, feature, environment branches) | Trung bình | Cần staging/pre-prod environment |
| Trunk-Based | 1–2 (main, short-lived feature tùy chọn) | Thấp nhất | Team mature, CI/CD mạnh, deploy on-demand |
So sánh chi tiết: Git Flow vs Trunk-Based Development
| Tiêu chí | Git Flow | Trunk-Based Development |
|---|---|---|
| Branch lifetime | Feature branch: tuần → tháng | Short-lived: giờ → max 2 ngày |
| Merge frequency | Khi feature "xong" | Ít nhất 1 lần/ngày |
| PR size | Hàng trăm → ngàn dòng | Vài chục → vài trăm dòng |
| Merge conflicts | Thường xuyên, phức tạp | Hiếm, dễ resolve |
| Main branch state | Chỉ chứa released code | Luôn deployable |
| Release process | Release branch → QA → merge main | Deploy từ main bất cứ lúc nào |
| Hotfix | Nhánh hotfix riêng, merge cả main + develop | Fix trực tiếp trên main, deploy ngay |
| Feature chưa hoàn thành | Ở trên feature branch, chưa merge | Merge vào main, ẩn sau feature flag |
| CI/CD fit | CI chạy trên develop, CD phụ thuộc release branch | CI/CD chạy liên tục trên main |
| DORA performance | Medium → Low performers | High → Elite performers |
Feature Flags — Chìa khóa cho Trunk-Based Development
Feature flags (hay feature toggles) là kỹ thuật cho phép bật/tắt tính năng mà không cần deploy lại. Đây là thành phần thiết yếu giúp TBD hoạt động an toàn — developer có thể merge code chưa hoàn thành vào main mà không ảnh hưởng user.
flowchart LR
DEV["Developer merge
feature chưa xong"] --> MAIN["main branch"]
MAIN --> DEPLOY["Deploy to Production"]
DEPLOY --> FF{"Feature Flag
ON / OFF?"}
FF -->|ON cho internal| BETA["Beta Users
thấy feature mới"]
FF -->|OFF cho public| PROD["Public Users
không thấy gì"]
FF -->|Gradual rollout| CANARY["5% → 25% → 100%"]
style MAIN fill:#e94560,stroke:#fff,color:#fff
style FF fill:#ff9800,stroke:#fff,color:#fff
style BETA fill:#4CAF50,stroke:#fff,color:#fff
style PROD fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style CANARY fill:#2196F3,stroke:#fff,color:#fff
Hình 5: Feature flags cho phép tách deployment khỏi release — deploy mọi lúc, release khi sẵn sàng
Các loại Feature Flag
| Loại | Mục đích | Lifetime | Ví dụ |
|---|---|---|---|
| Release Flag | Ẩn feature chưa hoàn thành | Ngắn (xóa sau khi release) | NEW_CHECKOUT_FLOW |
| Experiment Flag | A/B testing | Trung bình | PRICING_PAGE_V2 |
| Ops Flag | Circuit breaker, kill switch | Dài hạn | ENABLE_ELASTICSEARCH |
| Permission Flag | Entitlement, premium feature | Vĩnh viễn | ADVANCED_ANALYTICS |
✓ Quy tắc vàng: Dọn dẹp Feature Flags
Release flags là "nợ kỹ thuật có kế hoạch" — sau khi feature stable và rollout 100%, phải xóa flag và code path cũ. Team nên đặt expiration date cho mỗi flag (ví dụ: 30 ngày sau khi 100% rollout). Các công cụ như LaunchDarkly, Flagsmith, hay OpenFeature đều hỗ trợ flag lifecycle management.
DORA Metrics và Branching Strategy
Nghiên cứu DORA (DevOps Research and Assessment) từ Google cung cấp dữ liệu thực tế về mối quan hệ giữa branching strategy và delivery performance. Từ năm 2025, DORA framework mở rộng thêm metric thứ 5 — Rework Rate — và chuyển từ 4 tầng (Elite/High/Medium/Low) sang 7 team archetypes dựa trên cả delivery performance lẫn human factors.
flowchart TB
subgraph DORA["5 DORA Metrics (2025+)"]
DF["Deployment
Frequency"]
LT["Lead Time
for Changes"]
CFR["Change Failure
Rate"]
MTTR["Mean Time
to Recovery"]
RR["Rework Rate
(mới 2025)"]
end
subgraph ELITE["Elite Performers"]
E1["Deploy: nhiều lần/ngày"]
E2["Lead time: < 1 giờ"]
E3["Failure rate: 0-5%"]
E4["Recovery: < 1 giờ"]
E5["Rework: < 10%"]
end
TBD["Trunk-Based
Development"] -->|"enables"| ELITE
style TBD fill:#e94560,stroke:#fff,color:#fff
style DF fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style LT fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style CFR fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style MTTR fill:#f8f9fa,stroke:#e94560,color:#2c3e50
style RR fill:#f8f9fa,stroke:#e94560,color:#2c3e50
Hình 6: 5 DORA Metrics và benchmark của Elite Performers — TBD là nền tảng cho hiệu suất cao
📊 Dữ liệu từ DORA Research
Chuyển từ Medium lên High performance yêu cầu chuyển sang trunk-based development hoặc short-lived feature branches. Elite teams chia sẻ đặc điểm chung: không có long-lived feature branches, automated tests chạy trên mỗi commit, batch size nhỏ, và deployment pipeline đủ nhanh và tin cậy để chạy như thói quen hàng ngày.
Khi nào chọn Git Flow, khi nào chọn TBD?
flowchart TD
START["Team của bạn"] --> Q1{"Deploy frequency?"}
Q1 -->|"Hàng ngày /
nhiều lần/ngày"| TBD_REC["→ Trunk-Based Development"]
Q1 -->|"Hàng tuần /
hàng tháng"| Q2{"Cần maintain
nhiều version?"}
Q2 -->|Có| GF_REC["→ Git Flow"]
Q2 -->|Không| Q3{"Team size?"}
Q3 -->|"≤ 5 người"| GHF_REC["→ GitHub Flow"]
Q3 -->|"> 5 người"| Q4{"Có staging
environment?"}
Q4 -->|Có| GLF_REC["→ GitLab Flow"]
Q4 -->|Không| TBD_REC2["→ Trunk-Based
+ Feature Flags"]
style TBD_REC fill:#4CAF50,stroke:#fff,color:#fff
style TBD_REC2 fill:#4CAF50,stroke:#fff,color:#fff
style GF_REC fill:#2196F3,stroke:#fff,color:#fff
style GHF_REC fill:#ff9800,stroke:#fff,color:#fff
style GLF_REC fill:#9C27B0,stroke:#fff,color:#fff
style START fill:#e94560,stroke:#fff,color:#fff
Hình 7: Decision tree chọn branching strategy dựa trên đặc điểm team và product
| Tình huống | Khuyến nghị | Lý do |
|---|---|---|
| Web app SaaS, deploy liên tục | Trunk-Based Development | Main luôn deployable, feedback nhanh |
| Mobile app (iOS/Android), release qua App Store | Git Flow hoặc GitLab Flow | Cần release branch cho từng version, hotfix cho version cũ |
| Open-source library, nhiều version | Git Flow | Maintain multiple release lines (v1.x, v2.x) |
| Startup nhỏ, ít process | GitHub Flow | Đơn giản nhất, ít overhead |
| Enterprise, compliance requirements | GitLab Flow + Environment branches | Audit trail rõ ràng qua environment promotion |
| Team lớn (>20 dev), monorepo | Trunk-Based + Feature Flags | Tránh branch explosion, merge conflict |
Chuyển đổi từ Git Flow sang Trunk-Based Development
Nếu team đang dùng Git Flow và muốn chuyển sang TBD, đây là lộ trình từng bước thay vì "big bang":
⚠ Đừng bỏ qua giai đoạn 2
Trunk-Based Development mà không có automated testing mạnh sẽ gây hỗn loạn. Khi tất cả developer commit vào cùng một nhánh, code lỗi ảnh hưởng toàn team ngay lập tức. Test suite là prerequisite, không phải nice-to-have.
Best Practices cho Trunk-Based Development
1. Small Batch Size
Mỗi PR nên thay đổi dưới 400 dòng code. Nghiên cứu từ Google cho thấy PR trên 400 dòng có review quality giảm đáng kể — reviewer bắt đầu skim thay vì đọc kỹ. Chia feature lớn thành nhiều PR liên tiếp, mỗi PR tự chứa và deployable.
2. Pre-commit Checks
# .husky/pre-commit hoặc CI pipeline
npm run lint
npm run type-check
npm run test:unit -- --changed
Chạy lint, type-check, và unit test trên code thay đổi trước khi push. Giảm thời gian CI feedback từ phút xuống giây.
3. Branch Protection Rules
# GitHub branch protection cho main
- Require pull request reviews (≥ 1 approval)
- Require status checks to pass (CI pipeline)
- Require branches to be up to date
- Do NOT require linear history (cho phép merge commits)
4. Commit Message Convention
# Conventional Commits
feat(auth): add OAuth2 login flow
fix(cart): resolve race condition in quantity update
refactor(api): extract validation middleware
chore(deps): bump express to 4.21.0
Conventional Commits giúp auto-generate changelog, semantic versioning, và dễ đọc git log.
5. Ship/Show/Ask Framework
| Kiểu | Hành động | Khi nào dùng |
|---|---|---|
| Ship | Merge thẳng, không cần review | Fix typo, update docs, bump dependencies |
| Show | Merge rồi mở PR để team xem | Refactor nhỏ, change rõ ràng, confident about correctness |
| Ask | Mở PR, chờ review rồi mới merge | Feature mới, thay đổi architecture, code phức tạp |
Thực tế tại các công ty lớn
Tổng kết
Không có branching strategy nào là "đúng" cho mọi team. Git Flow vẫn hợp lý cho software cần versioning rõ ràng và scheduled releases (mobile app, SDK, enterprise). Nhưng cho web application, SaaS, và bất kỳ team nào muốn deploy thường xuyên, Trunk-Based Development kết hợp feature flags là lựa chọn được DORA research chứng minh hiệu quả.
Điều quan trọng nhất không phải chọn mô hình nào, mà là team có automated testing đủ mạnh để tự tin merge code thường xuyên, và culture tin tưởng để mọi người commit nhỏ, review nhanh, và ship liên tục.
Nguồn tham khảo:
Disclaimer: The opinions expressed in this blog are solely my own and do not reflect the views or opinions of my employer or any affiliated organizations. The content provided is for informational and educational purposes only and should not be taken as professional advice. While I strive to provide accurate and up-to-date information, I make no warranties or guarantees about the completeness, reliability, or accuracy of the content. Readers are encouraged to verify the information and seek independent advice as needed. I disclaim any liability for decisions or actions taken based on the content of this blog.