Skip to content
On this page

Git 工作流模式

Git工作流模式定义了团队如何使用Git进行协作开发的规范。本章详细介绍各种流行的工作流模式和最佳实践。

工作流基础概念

什么是工作流

Git工作流是团队协作开发的约定和规范,定义了分支管理、代码提交、代码审查和发布流程等。

工作流选择因素

选择合适的工作流需要考虑:

  • 团队规模
  • 项目复杂度
  • 发布频率
  • 团队经验
  • 工具链支持

集中式工作流

基本概念

集中式工作流是最简单的Git工作流,类似传统的SVN工作方式。

bash
# 集中式工作流基本操作
# 1. 获取最新代码
git pull origin main

# 2. 开发
git add .
git commit -m "Add new feature"

# 3. 推送更改
git push origin main

集中式工作流最佳实践

bash
# 配置工作流
git config --global pull.rebase true  # 使用rebase而不是merge
git config --global push.default simple

# 工作流程
# 1. 每次开始工作前拉取最新代码
git pull --rebase

# 2. 开发并提交
git add .
git commit -m "Feature: add user authentication"

# 3. 推送前再次拉取
git pull --rebase
git push origin main

功能分支工作流

基本概念

功能分支工作流为每个功能或任务创建独立的分支。

bash
# 功能分支工作流基本操作
# 1. 从main创建功能分支
git checkout main
git pull origin main
git checkout -b feature/user-authentication

# 2. 开发功能
git add .
git commit -m "Implement login functionality"

# 3. 推送功能分支
git push -u origin feature/user-authentication

# 4. 创建Pull Request/Merge Request

# 5. 合并后清理
git checkout main
git pull origin main
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication

功能分支最佳实践

bash
# 分支命名规范
feature/user-authentication
bugfix/login-error
hotfix/security-patch
release/v1.2.0

# 保持功能分支更新
git checkout feature/user-authentication
git fetch origin
git rebase origin/main

# 频繁推送以备份
git push origin feature/user-authentication

# 提交信息规范
git commit -m "feat: add user authentication module"
git commit -m "fix: resolve login error in authentication"
git commit -m "docs: update authentication API documentation"

Git Flow工作流

Git Flow基础

Git Flow定义了严格的分支模型,适合有计划发布周期的项目。

bash
# Git Flow分支结构
# main: 生产环境代码
# develop: 开发环境代码
# feature/*: 功能开发
# release/*: 发布准备
# hotfix/*: 紧急修复

# 使用git flow工具(需要安装)
# 初始化
git flow init

# 开发功能
git flow feature start user-authentication
git add .
git commit -m "Implement user auth"
git flow feature finish user-authentication

# 准备发布
git flow release start v1.2.0
git flow release finish v1.2.0

# 紧急修复
git flow hotfix start security-patch
git flow hotfix finish security-patch

手动实现Git Flow

bash
# 开发功能
git checkout develop
git pull origin develop
git checkout -b feature/user-authentication
# 开发...
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication

# 准备发布
git checkout -b release/v1.2.0 develop
# 测试和修复bug...
git checkout main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"
git checkout develop
git merge --no-ff release/v1.2.0
git branch -d release/v1.2.0

# 紧急修复
git checkout -b hotfix/security-patch main
# 修复bug...
git checkout main
git merge --no-ff hotfix/security-patch
git tag -a v1.2.1 -m "Hotfix security patch"
git checkout develop
git merge --no-ff hotfix/security-patch
git branch -d hotfix/security-patch

GitHub Flow工作流

GitHub Flow基础

GitHub Flow是一种简化的Git Flow,适合持续部署的项目。

bash
# GitHub Flow基本流程
# 1. 从main创建分支
git checkout main
git pull origin main
git checkout -b feature/user-authentication

# 2. 开发并推送
git add .
git commit -m "Add user authentication"
git push -u origin feature/user-authentication

# 3. 创建Pull Request

# 4. 代码审查

# 5. 合并Pull Request

# 6. 清理分支
git checkout main
git pull origin main
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication

GitHub Flow最佳实践

bash
# 配置GitHub Flow
git config --global pull.rebase true

# 分支策略
# - 保持main分支始终可部署
# - 从main创建功能分支
# - 频繁同步main分支到功能分支
# - 使用Pull Request进行代码审查

# 保持功能分支更新
git checkout feature/user-authentication
git fetch origin
git rebase origin/main

# 频繁推送小的提交
git add .
git commit -m "WIP: working on auth"
git push origin feature/user-authentication

GitLab Flow工作流

GitLab Flow基础

GitLab Flow结合了Git Flow和GitHub Flow的优点,使用环境分支。

bash
# GitLab Flow分支结构
# main: 主开发分支
# pre-production: 预发布环境
# production: 生产环境

# 开发流程
git checkout main
git pull origin main
git checkout -b feature/user-authentication
# 开发...
git checkout main
git merge --no-ff feature/user-authentication

# 部署到预发布环境
git checkout pre-production
git merge --no-ff main
git push origin pre-production

# 部署到生产环境
git checkout production
git merge --no-ff main
git push origin production

Forking工作流

Forking工作流基础

Forking工作流适用于开源项目或跨组织协作。

bash
# Forking工作流基本操作
# 1. Fork上游仓库
# 2. 克隆自己的fork
git clone git@github.com:your-username/project.git
cd project

# 3. 添加上游仓库
git remote add upstream https://github.com/original/project.git

# 4. 同步上游更改
git fetch upstream
git checkout main
git merge upstream/main

# 5. 开发功能
git checkout -b feature/new-feature
# 开发...
git push origin feature/new-feature

# 6. 创建Pull Request

# 7. 同步上游更改到功能分支
git checkout feature/new-feature
git fetch upstream
git rebase upstream/main

Forking工作流最佳实践

bash
# 配置Forking工作流
git remote add upstream https://github.com/original/project.git
git remote -v  # 验证远程仓库配置

# 定期同步上游仓库
git checkout main
git pull upstream main
git push origin main

# 保持功能分支与上游同步
git checkout feature/new-feature
git fetch upstream
git rebase upstream/main
git push -f origin feature/new-feature  # 需要强制推送

工作流选择指南

小团队/个人项目

bash
# 推荐:功能分支工作流或GitHub Flow
# 简单直接,适合快速迭代
git checkout main
git pull origin main
git checkout -b feature/new-feature
# 开发...
git push -u origin feature/new-feature
# 创建PR/MR...
git checkout main
git pull origin main
git branch -d feature/new-feature

中等规模团队

bash
# 推荐:GitHub Flow + 环境分支
# 结合了简单性和环境管理

# 分支策略
# main: 可部署代码
# develop: 集成分支
# feature/*: 功能分支
# release/*: 发布分支

# 开发流程
git checkout develop
git pull origin develop
git checkout -b feature/new-feature
# 开发...
git checkout develop
git merge --no-ff feature/new-feature
# 部署到预发布环境...
# 合并到main并部署到生产...

大型团队/企业项目

bash
# 推荐:Git Flow或GitLab Flow
# 提供了完整的发布管理和环境隔离

# 分支策略
# main: 生产代码
# develop: 开发集成
# feature/*: 功能开发
# release/*: 发布准备
# hotfix/*: 紧急修复
# support/*: 支持分支

# 配置保护分支和合并策略
# - main分支受保护
# - 需要代码审查
# - 需要CI/CD通过
# - 需要状态检查

工作流自动化

CI/CD集成

yaml
# .github/workflows/feature-branch.yml
name: Feature Branch CI
on:
  push:
    branches-ignore:
      - main
      - develop
  pull_request:
    branches:
      - main
      - develop

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'
      - run: npm ci
      - run: npm test
      - run: npm run build

自动分支清理

bash
# 自动清理已合并的分支
git fetch --prune
git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d

# 使用Git别名
git config --global alias.cleanup '!git fetch --prune && git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d'

工作流工具

Git别名配置

bash
# 工作流相关别名
git config --global alias.pr '!git checkout -b "$1" && git push -u origin "$1"'
git config --global alias.sync '!git fetch origin && git rebase origin/main'
git config --global alias.cleanup '!git fetch --prune && git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d'
git config --global alias.review '!git checkout main && git pull origin main && git checkout - && git rebase main'

钩子脚本

bash
# .git/hooks/pre-push
#!/bin/sh
# 阻止直接推送到main分支
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
  echo "禁止直接推送到main分支,请使用Pull Request"
  exit 1
fi

工作流最佳实践

提交信息规范

bash
# 使用约定式提交
git commit -m "feat: add user authentication module"
git commit -m "fix: resolve login error in auth module"
git commit -m "docs: update authentication API documentation"
git commit -m "style: format authentication code"
git commit -m "refactor: improve auth module structure"
git commit -m "test: add authentication unit tests"
git commit -m "chore: update dependencies"

代码审查最佳实践

bash
# Pull Request模板
# .github/pull_request_template.md
"""
## 变更描述
简要描述本次变更的内容

## 相关问题
- 关闭 #issue-number

## 测试验证
- [ ] 单元测试通过
- [ ] 集成测试通过
- [ ] 手动测试验证

## 变更类型
- [ ] 新功能
- [ ] Bug修复
- [ ] 文档更新
- [ ] 重构
"""

选择合适的工作流并坚持执行,是团队协作成功的关键。根据项目特点和团队规模选择最适合的工作流模式。