如果你在开发团队中工作,很可能听说过 Gitflow。或许你甚至经常使用它。但是,你真的知道何时使用它,何时应该避免使用它吗?
在这篇文章中,我不仅要教你标准的工作流程,还会展示真实的案例研究、现代替代方案,以及如何高效地实现它。作为一名在敏捷初创公司和大公司都工作过的开发者,我见证过 Gitflow 的成功,也见过它灾难性的失败。
Gitflow 是由 Vincent Driessen 在 2010 年左右创建的一种分支策略。它不是 Git 提供的工具,而是一种关于如何使用分支的约定。其主要结构包括:
main/master # 生产环境 - 仅稳定代码
develop # 集成环境 - 下一个发布版本feature/* # 新功能开发
release/* # 为生产环境做准备
hotfix/* # 生产环境中的紧急修复场景:你的团队需要为应用添加 OAuth2 身份验证。
#1. 从 develop 分支创建功能分支
git checkout develop
git pull origin develop
git checkout -b feature/oauth2-integration
#2. 在功能分支上工作(多次提交)
git add .
git commit -m "Add Google OAuth2 basic setup"
git commit -m "Implement token refresh logic"
git commit -m "Add user profile sync"
#3. 完成功能
git checkout develop
git merge --no-ff feature/oauth2-integration
git branch -d feature/oauth2-integration
git push origin develop为了将功能分支的历史保留为一个逻辑组。
场景:版本 2.1.0 已准备好投入生产。
#1. 创建发布分支
git checkout develop
git checkout -b release/2.1.0
#2. 准备发布(更新版本号、文档等)
# 更新 package.json 中的版本号
git commit -am "Bump version to 2.1.0"
#3. 合并到 main 和 develop 分支
git checkout main
git merge --no-ff release/2.1.0
git tag -a v2.1.0 -m "Release version 2.1.0"
git checkout develop
git merge --no-ff release/2.1.0
#4. 删除发布分支
git branch -d release/2.1.0场景:在生产环境中发现严重安全漏洞。
#1. 从 main 分支创建热修复分支
git checkout main
git checkout -b hotfix/security-patch
#2. 修复问题
# 应用安全修复
git commit -am "Fix SQL injection vulnerability"
#3. 应用到 main 和 develop 分支
git checkout main
git merge --no-ff hotfix/security-patch
git tag -a v2.1.1 -m "Security hotfix"
git checkout develop
git merge --no-ff hotfix/security-patch
#4. 删除热修复分支
git branch -d hotfix/security-patch# 1. 初始化仓库
git init
git add .
git commit -m "Initial commit"
# 2. 创建主分支
git branch develop
git checkout develop
# 3. 配置上游(远程仓库)
git remote add origin <url>
git push -u origin main
git push -u origin develop{
"scripts":{
"feature:start": "git checkout develop && git pull origin develop && git checkout -b",
"feature:finish": "git checkout develop && git merge --no-ff",
"release:start":"git checkout develop && git checkout -b release/",
"hotfix:start":"git checkout main && git checkout -b hotfix/"
}
}name: Gitflow CI
on:
push:
branches: [develop, main, feature/*, release/*, hotfix/*]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
branch-type: [feature, release, hotfix]
steps:
- uses: actions/checkout@v3
- name: Detect branch type
id: branch-type
run: |
if [[ ${{ github.ref }} == refs/heads/feature/* ]]; then
echo "Type=feature" >> $GITHUB_ENV
elif [[ ${{ github.ref }} == refs/heads/release/* ]]; then
echo "TYPE=release" >> $GITHUB_ENV
fi
- name: Run tests
if: env.TYPE == 'feature'
run: npm run test:unit
- name: Integration tests
if: env.TYPE == 'release'
run: npm run test:integration# 功能:具体功能描述
feature/user-dashboard
feature/payment-processing
# 发布:语义化版本号
release/1.2.0
release/2.0.0-rc.1
# 热修复:简短描述
hotfix/login-error
hotfix/security-patch-cve-2024Gitflow 并未消亡,而是在进化。对于 2026 年,建议:
个人建议:如果你的团队刚开始使用 Git,可以从 Gitflow 入手,但要保持开放的心态,以便在未来演进并转向更简单的新工作流。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。