Git branching strategy là cách tổ chức các branch để quản lý code hiệu quả. Không có strategy nào đúng cho tất cả, nhưng có những nguyên tắc giúp team làm việc mượt hơn. Bài viết này so sánh các chiến lược phổ biến và hướng dẫn cách chọn phù hợp cho team.

Tại sao cần Branching Strategy?

  • Tránh conflict: Nhiều người làm cùng lúc không lo đè code nhau.
  • Code sạch: Main branch luôn deploy được, không có code half-done.
  • Review dễ: Pull request rõ ràng, reviewer hiểu context.
  • Rollback nhanh: Lỗi ở đâu thì revert ở đó.

Các Branch chính

Mọi strategy đều có các branch cốt lõi:

  • main/master: Code đang chạy production. Không push trực tiếp.
  • develop: Integration branch cho các feature. Từ đây release.
  • feature/*: Branch phát triển tính năng mới.
  • hotfix/*: Sửa lỗi production gấp.
  • release/*: Chuẩn bị release.

1. Git Flow

Git Flow là strategy cổ điển, phù hợp cho release schedule có định kỳ.

main ────────────────────────────────●─────────────────────────
        ↑                       ↑                    ↑
        │                       │                    │
        │         release/v1.0 ─┘         release/v2.0
        │              ↑                      ↑
        │              │                      │
develop ──────────●────────────●────────────────────────
        │         ↑              ↑           ↑
        │         │              │           │
        │    feature/A      feature/B    feature/C
        │         ↑              ↑
        │         │              │
        └────────┴──────────────┘
              (merge về develop)

hotfix/*: branch từ main, merge lại main và develop

Commands

# Start feature
git checkout develop
git pull origin develop
git checkout -b feature/user-auth

# Hoàn thành feature
git checkout develop
git merge --no-ff feature/user-auth
git branch -d feature/user-auth
git push origin develop

# Tạo release
git checkout develop
git checkout -b release/v1.0.0
# ... test và fix ...
git checkout main
git merge --no-ff release/v1.0.0
git tag v1.0.0
git push origin main --tags
git branch -d release/v1.0.0

# Hotfix
git checkout main
git checkout -b hotfix/critical-bug
# ... fix ...
git checkout main
git merge --no-ff hotfix/critical-bug
git push origin main
git checkout develop
git merge --no-ff hotfix/critical-bug
git branch -d hotfix/critical-bug

2. GitHub Flow

Đơn giản hơn Git Flow, phù hợp cho continuous delivery. Chỉ có main và feature branches.

main ────────────────────────────●──────●──────●──────●──
        │                   ↑      │      ↑      │
        │                   │      │      │      │
        └── feature ──────▶│      │      │      │
                     ↑      │      │      │      │
                     │──────┘      │      │      │
                     │             │      │      │
                     └────────────▶│      │      │
                                   │      │      │
                                   └──────┘      │
                                                 │
                              pull/merge request ─┘

Commands

# Tạo branch từ main
git checkout main
git pull origin main
git checkout -b feature/new-feature

# Làm việc và commit
git add .
git commit -m "Add new feature"

# Push và tạo PR
git push -u origin feature/new-feature

# Sau khi PR approved và merged
git checkout main
git pull origin main
git branch -d feature/new-feature

3. Trunk-Based Development

Tất cả dev commit vào main (trunk) thường xuyên. Phù hợp cho CI/CD và small teams.

main ───●───●───●───●───●───●───●───●───●───●───●───●───
           ↑   ↑   ↑   ↑   ↑   ↑   ↑   ↑   ↑   ↑   ↑
           │   │   │   │   │   │   │   │   │   │   │
           └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───
               (short-lived feature branches)

Nguyên tắc

  • Feature branches sống dưới 2 ngày.
  • Commit nhỏ, merge thường xuyên.
  • Dùng feature flags thay vì branches.
  • li>CI/CD chạy sau mỗi commit.

4. So sánh các Strategy

Tiêu chíGit FlowGitHub FlowTrunk-Based
Độ phức tạpCaoThấpThấp
Release scheduleĐịnh kỳLiên tụcLiên tục
Phù hợpSản phẩm có versionWeb/SaaSStartup/Fast-paced
Team sizeLớnTrung bìnhNhỏ
HotfixDễTrung bìnhKhó

5. Naming Conventions

# Feature
feature/user-authentication
feature/payment-integration
feature/dark-mode

# Bugfix
bugfix/login-redirect-error
bugfix/memory-leak-fix

# Hotfix
hotfix/security-patch-2024
hotfix/production-crash

# Release
release/v1.2.0
release/v2.0.0-beta

# Changelog
changelog/update-dependencies
changelog/deprecate-old-api

6. Git Aliases

# ~/.gitconfig

[alias]
    # Start feature
    feat = checkout -b feature/

    # Start hotfix
    hotfix = checkout -b hotfix/

    # Smart log
    lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

    # Undo last commit (keep changes)
    undo = reset --soft HEAD~1

    # Clean merged branches
    cleanup = !git branch --merged | grep -v '\\*\\|main\\|develop' | xargs -r git branch -d

    # Push new branch
    push-new = !git push -u origin HEAD

7. Commit Messages

# Format
(): 



# Types feat: new feature fix: bug fix docs: documentation style: formatting, no code change refactor: code change that neither fixes nor adds feature test: adding tests chore: maintenance # Ví dụ feat(auth): add Google OAuth login Implement Google OAuth 2.0 authentication flow. - Add Google provider config - Create OAuth callback handler - Update user model Closes #123

8. Pull Request Guidelines

  • Title rõ ràng: "Add user authentication with JWT"
  • Mô tả đầy đủ: Đã làm gì, tại sao, cách test
  • Link issue: "Fixes #123"
  • Screenshots: Trước/sau nếu có UI thay đổi
  • Reviewers: 2 người tối thiểu

9. GitHub Actions cho Branching

name: Branch Protection

on:
  pull_request:
    branches: [ main, develop ]

jobs:
  protect:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - name: Check branch name
      run: |
        if [[ ! "${{ github.head_ref }}" =~ ^(feature|bugfix|hotfix|release)/ ]]; then
          echo "Branch name must start with feature/, bugfix/, hotfix/, or release/"
          exit 1
        fi

10. Quy tắc cho Team

  • [ ] Không push trực tiếp vào main: Luôn qua PR.
  • [ ] Merge nhanh: Feature branch không nên sống quá 1 tuần.
  • [ ] Rebase thay vì merge: Giữ lịch sử sạch hơn.
  • [ ] Xóa branch sau merge: Dọn dẹp thường xuyên.
  • [ ] Commit nhỏ: Mỗi commit nên làm một việc.

Kết Luận

  • Git Flow: Cho sản phẩm có release schedule rõ ràng.
  • GitHub Flow: Cho web app, CI/CD liên tục.
  • Trunk-Based: Cho startup, team nhỏ, cần tốc độ.

Chọn strategy phù hợp với team và thực hiện nghiêm túc. Kết hợp với GitHub Actions CI/CD để automate quy trình.

Chào các bạn mình là Quốc Hùng , mình sinh ra thuộc cung song tử ,song tử luôn khẳng định chính mình ,luôn luôn phấn đấu vượt lên phía trước ,mình sinh ra và lớn lên tại vùng đất võ cổ truyền ,đam mê của mình là coder ,ngày đi học tối về viết blog ...