好的 Git 工流不只是分支命名规范,更是一套让团队协作高效、发布安全可控的完整体系。本文分享适合小团队(3-5人)的简化 Git Flow 和 CI/CD 配置。

GitHub Actions — CI Pipeline
📥
Checkout
3s
📦
Install
28s
🔍
Lint
12s
🧪
Test
45s
🏗
Build
35s
🚀
Deploy
18s
总耗时: 2m 21s · ✅ 全部通过

一、分支策略

小团队不需要完整的 Git Flow。采用简化版:

分支模型 — 简化 Git Flow
main ──────────── ● ──────── ● ──── v1.2.0
                 │
develop ──── ● ── ● ── ● ── ● ── ● ──
              │            │
feat/login  ● ── ●            │
                             │
feat/api                 ● ── ●

二、Commit 规范

使用 Conventional Commits,方便自动生成 CHANGELOG:

# 格式
type(scope): description

# 示例
feat(auth): 添加微信扫码登录
fix(order): 修复金额计算精度问题
docs(api): 更新接口文档
chore: 升级依赖到最新版本

三、GitHub Actions CI/CD

# .github/workflows/ci.yml
name: CI
on:
  push: { branches: [main, develop] }
  pull_request: { branches: [develop] }

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

      - uses: actions/setup-node@v4
        with: { node-version: 20, cache: 'pnpm' }

      - run: pnpm install --frozen-lockfile

      - name: Lint
        run: pnpm lint

      - name: Test
        run: pnpm test -- --coverage

      - name: Build
        run: pnpm build

      - name: Deploy
        if: github.ref == 'refs/heads/main'
        run: |
          ssh ${{ secrets.SERVER }} \
            "cd /app && git pull && docker compose up -d --build"

四、Pre-commit Hooks

💡 推荐搭配:使用 husky + lint-staged,在提交前自动格式化和检查,避免 CI 因为格式问题失败。
// package.json
{
  "lint-staged": {
    "*.{js,ts}": ["eslint --fix", "prettier --write"],
    "*.{json,md}": ["prettier --write"]
  }
}

五、总结