GitHub Flow is a lightweight branching model centered on `main` as the deployable branch. Every change starts as a branch, goes through a pull request, gets reviewed, and merges back to `main`.
Core rules:
# typical lifecycle
git checkout main
git pull origin main
git checkout -b feature/add-search
# ... make changes ...
git push -u origin feature/add-search
# open PR → review → merge → deploy
git checkout main
git pull origin main
git checkout -b feature/user-profile
git push -u origin feature/user-profile
Branch always from the latest `main`. Keep branches short-lived—merge within hours or a few days, not weeks.
# update your branch with latest main before PR
git checkout feature/user-profile
git fetch origin
git rebase origin/main
git push --force-with-lease
git add .
git commit -m "feat: add user profile page"
git push origin feature/user-profile
# amend and force-push safely
git commit --amend -m "feat: add user profile page with avatar"
git push --force-with-lease origin feature/user-profile
Use Conventional Commits for traceability:
git commit -m "feat: add search endpoint"
git commit -m "fix: handle empty query in search"
git commit -m "refactor: extract search logic to service"
# push branch then create PR via GitHub CLI
gh pr create --title "feat: add user profile" --body "## Changes
- Add profile page component
- Add avatar upload
- Add profile API endpoint
Closes #42"
# draft PR for early feedback
gh pr create --draft --title "WIP: new search feature"
# convert draft to ready
gh pr ready 123
# list and view PRs
gh pr list
gh pr view 123
gh pr view 123 --web
# request reviewers
gh pr edit 123 --add-reviewer alice,bob
# check review status
gh pr view 123 --json reviews
# approve from CLI
gh pr review 123 --approve --body "LGTM!"
# request changes
gh pr review 123 --request-changes --body "Please add unit tests"
Review checklist:
# checkout a PR locally to test it
gh pr checkout 123
# run tests locally
npm test
# go back to your branch
git checkout feature/user-profile
# view CI status for a PR
gh pr checks 123
# watch checks in real-time
gh pr checks 123 --watch
# view detailed check info
gh api repos/OWNER/REPO/commits/SHA/check-runs
Configure required status checks in repo Settings → Branches → Branch protection rules. Common CI workflows:
# .github/workflows/ci.yml
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
- run: npm run lint
# merge PR (creates merge commit)
gh pr merge 123 --merge
# squash merge (preferred for clean history)
gh pr merge 123 --squash
# rebase merge (linear history)
gh pr merge 123 --rebase
# delete remote branch after merge
git push origin --delete feature/user-profile
Deploy after merge:
# pull latest main and deploy
git checkout main
git pull origin main
# tag the release
git tag -a v1.2.0 -m "Release 1.2.0: user profile feature"
git push origin v1.2.0
# trigger deployment
gh workflow run deploy.yml -f environment=production
# create annotated tag on main
git tag -a v1.1.0 -m "Release 1.1.0"
git push origin v1.1.0
# create GitHub Release with notes
gh release create v1.1.0 --title "v1.1.0" --notes "## What's New
- User profile page
- Search improvements
## Bug Fixes
- Fixed login redirect"
# list releases
gh release list
# download release assets
gh release download v1.1.0
# generate changelog from commits
git log v1.0.0..v1.1.0 --oneline --no-merges
gh api repos/OWNER/REPO/compare/v1.0.0...v1.1.0 --jq '.commits[] | .commit.message'
# feature branches
feature/add-search
feature/user-profile
# bug fix branches
fix/login-redirect
fix/null-pointer-on-save
# other prefixes
hotfix/critical-security-patch
chore/upgrade-dependencies
docs/update-api-guide
refactor/extract-auth-middleware
test/add-user-service-tests
Keep names short, lowercase, hyphen-separated. The prefix tells the type, the rest tells the scope.
# view branch protection via API
gh api repos/OWNER/REPO/branches/main/protection
# set up protection via GitHub Settings:
# 1. Require PR before merging
# 2. Require approvals (1-2 reviewers)
# 3. Require status checks to pass
# 4. Require branches to be up to date
# 5. Require signed commits
# 6. Do not allow force pushes
# enforce with GitHub CLI (enterprise/org)
gh api repos/OWNER/REPO/branches/main/protection \
--method PUT \
--field required_status_checks='{"strict":true,"contexts":["ci/test"]}' \
--field required_pull_request_reviews='{"required_approving_review_count":1}' \
--field enforce_admins=true \
--field restrictions=null
# rebase on latest main to find conflicts early
git checkout feature/user-profile
git fetch origin
git rebase origin/main
# if conflicts occur, resolve each file
# edit conflicted files, then:
git add <resolved-file>
git rebase --continue
# push the resolved branch
git push --force-with-lease origin feature/user-profile
# alternatively, merge main into your branch
git checkout feature/user-profile
git fetch origin
git merge origin/main
# resolve conflicts, then:
git add .
git commit -m "merge: resolve conflicts with main"
git push origin feature/user-profile
# GitHub Flow — single long-lived branch (main)
main ← feature/a
main ← feature/b
main ← hotfix/c
# Git Flow — multiple long-lived branches
main ← tags only
develop ← integration branch
feature/* ← from develop
release/* ← from develop into main
hotfix/* ← from main back to main and develop
| Aspect | GitHub Flow | Git Flow | |--------|------------|----------| | Long-lived branches | `main` only | `main` + `develop` | | Release branches | No | Yes | | Complexity | Simple | Complex | | Best for | Continuous deployment | Scheduled releases | | Hotfix path | Branch from `main`, PR to `main` | Dedicated `hotfix/*` branch |
# start a new feature
git checkout main && git pull
git checkout -b feature/new-feature
# develop, commit, push, open PR
# hotfix workflow
git checkout main && git pull
git checkout -b hotfix/fix-payment-bug
# fix, commit, push, open PR, merge immediately
# sync long-running feature with main
git checkout feature/long-feature
git fetch origin
git rebase origin/main
git push --force-with-lease
# batch multiple small changes via squash
gh pr merge 123 --squash --subject "feat: add user settings module"
GitHub Flow 是一个轻量级分支模型,以 `main` 作为可部署分支。所有变更都从分支开始,经过 Pull Request 审查后合并回 `main`。
核心规则:
# 典型生命周期
git checkout main
git pull origin main
git checkout -b feature/add-search
# ... 编写代码 ...
git push -u origin feature/add-search
# 创建 PR → 审查 → 合并 → 部署
git checkout main
git pull origin main
git checkout -b feature/user-profile
git push -u origin feature/user-profile
始终从最新的 `main` 创建分支。保持分支短生命周期——几小时或几天内合并,不要拖几周。
# 创建 PR 前用最新 main 更新分支
git checkout feature/user-profile
git fetch origin
git rebase origin/main
git push --force-with-lease
git add .
git commit -m "feat: 添加用户资料页"
git push origin feature/user-profile
# 修改提交后安全地强制推送
git commit --amend -m "feat: 添加用户资料页(含头像)"
git push --force-with-lease origin feature/user-profile
使用 Conventional Commits 规范:
git commit -m "feat: 添加搜索接口"
git commit -m "fix: 处理搜索空查询"
git commit -m "refactor: 提取搜索逻辑到 service 层"
# 推送分支后通过 GitHub CLI 创建 PR
gh pr create --title "feat: 添加用户资料页" --body "## 变更内容
- 添加资料页组件
- 添加头像上传功能
- 添加资料 API 接口
Closes #42"
# 创建草稿 PR 获取早期反馈
gh pr create --draft --title "WIP: 新搜索功能"
# 将草稿转为正式 PR
gh pr ready 123
# 列出和查看 PR
gh pr list
gh pr view 123
gh pr view 123 --web
# 指定审查人
gh pr edit 123 --add-reviewer alice,bob
# 查看审查状态
gh pr view 123 --json reviews
# 通过 CLI 批准
gh pr review 123 --approve --body "LGTM!"
# 请求修改
gh pr review 123 --request-changes --body "请补充单元测试"
审查要点:
# 本地检出 PR 进行测试
gh pr checkout 123
# 本地运行测试
npm test
# 切回自己的分支
git checkout feature/user-profile
# 查看 PR 的 CI 状态
gh pr checks 123
# 实时监控检查
gh pr checks 123 --watch
# 查看详细的检查信息
gh api repos/OWNER/REPO/commits/SHA/check-runs
在仓库 Settings → Branches → Branch protection rules 中配置必须通过的状态检查。常见 CI 工作流:
# .github/workflows/ci.yml
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
- run: npm run lint
# 合并 PR(创建合并提交)
gh pr merge 123 --merge
# Squash 合并(推荐,保持历史整洁)
gh pr merge 123 --squash
# Rebase 合并(线性历史)
gh pr merge 123 --rebase
# 合并后删除远程分支
git push origin --delete feature/user-profile
合并后部署:
# 拉取最新 main 并部署
git checkout main
git pull origin main
# 打标签
git tag -a v1.2.0 -m "Release 1.2.0: 用户资料功能"
git push origin v1.2.0
# 触发部署
gh workflow run deploy.yml -f environment=production
# 在 main 上创建附注标签
git tag -a v1.1.0 -m "Release 1.1.0"
git push origin v1.1.0
# 创建 GitHub Release 并附上说明
gh release create v1.1.0 --title "v1.1.0" --notes "## 新功能
- 用户资料页
- 搜索功能改进
## 缺陷修复
- 修复登录重定向问题"
# 列出所有 Release
gh release list
# 下载 Release 资源
gh release download v1.1.0
# 从提交记录生成变更日志
git log v1.0.0..v1.1.0 --oneline --no-merges
gh api repos/OWNER/REPO/compare/v1.0.0...v1.1.0 --jq '.commits[] | .commit.message'
# 功能分支
feature/add-search
feature/user-profile
# 缺陷修复分支
fix/login-redirect
fix/null-pointer-on-save
# 其他前缀
hotfix/critical-security-patch
chore/upgrade-dependencies
docs/update-api-guide
refactor/extract-auth-middleware
test/add-user-service-tests
名称保持简短、小写、用连字符分隔。前缀表示类型,后面表示范围。
# 通过 API 查看分支保护规则
gh api repos/OWNER/REPO/branches/main/protection
# 通过 GitHub Settings 设置:
# 1. 合并前必须创建 PR
# 2. 必须获得审批(1-2 人)
# 3. 必须通过状态检查
# 4. 分支必须是最新的
# 5. 必须签名提交
# 6. 禁止强制推送
# 通过 GitHub CLI 设置保护规则(企业/组织)
gh api repos/OWNER/REPO/branches/main/protection \
--method PUT \
--field required_status_checks='{"strict":true,"contexts":["ci/test"]}' \
--field required_pull_request_reviews='{"required_approving_review_count":1}' \
--field enforce_admins=true \
--field restrictions=null
# 变基到最新 main 以尽早发现冲突
git checkout feature/user-profile
git fetch origin
git rebase origin/main
# 出现冲突时,逐个解决
# 编辑冲突文件后:
git add <resolved-file>
git rebase --continue
# 推送解决后的分支
git push --force-with-lease origin feature/user-profile
# 或者将 main 合并到功能分支
git checkout feature/user-profile
git fetch origin
git merge origin/main
# 解决冲突后:
git add .
git commit -m "merge: 解决与 main 的冲突"
git push origin feature/user-profile
# GitHub Flow — 单个长期分支(main)
main ← feature/a
main ← feature/b
main ← hotfix/c
# Git Flow — 多个长期分支
main ← 仅标签
develop ← 集成分支
feature/* ← 从 develop 创建
release/* ← 从 develop 合入 main
hotfix/* ← 从 main 修复合入 main 和 develop
| 方面 | GitHub Flow | Git Flow | |------|------------|----------| | 长期分支 | 仅 `main` | `main` + `develop` | | 发布分支 | 无 | 有 | | 复杂度 | 简单 | 复杂 | | 适用于 | 持续部署 | 定期发布 | | 热修路径 | 从 `main` 拉分支,PR 回 `main` | 专用 `hotfix/*` 分支 |
# 开始新功能
git checkout main && git pull
git checkout -b feature/new-feature
# 开发、提交、推送、创建 PR
# 热修流程
git checkout main && git pull
git checkout -b hotfix/fix-payment-bug
# 修复、提交、推送、创建 PR、立即合并
# 长期功能分支同步 main
git checkout feature/long-feature
git fetch origin
git rebase origin/main
git push --force-with-lease
# 通过 squash 合并多个小改动
gh pr merge 123 --squash --subject "feat: 添加用户设置模块"