Git Handbook

10 · Workflow Git thực tế

Từ feature, hotfix đến quy tắc collaboration và cách chọn branching strategy phù hợp team.

1. Feature branch workflow

git switch main
git fetch origin
git merge --ff-only origin/main

git switch -c feature/login
# code...
git status
git diff
git add -p
git diff --staged
git commit -m "feat(auth): add login"

git fetch origin
git rebase origin/main
# resolve + test
git push -u origin feature/login

Sau đó tạo Pull/Merge Request, review, CI và merge theo rule của team.

2. Hotfix khi đang có WIP

# đang ở feature/payment, code chưa xong
git stash push -u -m "WIP payment"

git switch main
git switch -c hotfix/checkout-timeout
# fix + test
git add -p
git commit -m "fix(checkout): handle gateway timeout"
git push -u origin hotfix/checkout-timeout

# quay lại
git switch feature/payment
git stash pop

Nếu thường xuyên phải làm song song, worktree có thể tốt hơn stash:

git worktree add ../project-hotfix -b hotfix/checkout main

3. Team rules nên thống nhất

Commit

Atomic, message rõ, test được, tránh commit debug/secret/generated files.

Branch

Quy ước tên: feature/, fix/, hotfix/, hoặc rule riêng của team.

History

Xác định team dùng merge commit, squash merge hay rebase merge.

Protection

Không force-push main/release branch; dùng branch protection và CI.

4. Các branching strategy phổ biến

StrategyĐặc điểmPhù hợp
Feature BranchMỗi feature/fix một branch ngắn hạn.Đa số team dùng PR.
GitHub FlowMain luôn deployable, branch ngắn, PR → deploy.Continuous delivery.
Trunk-Based DevelopmentBranch rất ngắn hoặc commit gần trunk; feature flags.CI/CD trưởng thành.
Git FlowMain/develop + feature/release/hotfix.Release cadence truyền thống; nhiều ceremony hơn.
Không có branching model “chuẩn cho mọi dự án”. Chọn model dựa trên release cadence, CI/CD, quy mô team và yêu cầu ổn định.

5. Checklist trước khi push/PR

  • git status sạch hoặc hiểu rõ file còn lại.
  • git diff --staged đúng scope.
  • Không commit secret.
  • Tests/lint/build cần thiết đã chạy.
  • Commit history đủ dễ review.
  • Feature branch đã cập nhật với base branch theo convention team.