Gitflow 流程示範
引入Giflow模式套用在Gitlab
主要因应工作上,协作流程,重新回温一下整个GitFlow流程与情境。
分支解说
长期分支 - master
分支、develop
分支
原因:因 Master 与 Develop 分支会一直存活在整个 Git flow 里,并不会被删除掉。
短期分支 - hotfix
分支、release
分支、feature
分支
原因:当完成专案后,这些更新的版本都会被合併进 Master 或 Develop 分支 ,之后就会被删除掉。
git init --initial-branch=main
git remote add origin git@gitlab.com:bda605/gitflowdemo.git
git add .
git commit -m "Initial commit"
git push -u origin main
第一步:
取得https://gitlab.com/your-username/your-repo.git
第二步:
git clone https://gitlab.com/your-username/your-repo.git
cd your-repo
git flow init
第三步骤:
git checkout -b develop
git push -u origin develop
在 GitLab 设定保护 (Protected Branches
) 以符合 Gitflow:
- 前往 GitLab 专案 →
Settings
→Repository
- 设定保护分支
main
只能由 管理员 或 特定角色 合併develop
只能透过 Merge Request (MR) 合併release/*
、hotfix/*
、feature/*
允许开发者提交,但仍需 MR
开发Feature流程
# 开发新功能分支并提交远端
git flow feature start feature-hello
git push -u origin feature/feature-hello
# 开发完成后并提交到远端
git add .
git commit -m "[FEATURE] 供单编号:001-追加hello讯息"
git push origin feature/feature-hello
在 GitLab 建立 Merge Request (MR)
- 前往 GitLab → 专案 → Merge Requests
- 选择
feature/feature-hello
合併到develop
- 提交 MR,请求 Code Review
- 通过 Review 后合併
完成后,最后再重新取develop版本
git checkout develop
git pull origin develop
注记:在开发过程中有develop新版本的时候
# 取develop从远端最新版本转入本机端的develop版本
git checkout develop
git pull origin develop
git checkout feature/feature-hello
git merge develop
# 底下遇到冲突、解决冲突就要提交一版
git add .
git commit -m "Merge latest develop into feature/feature-hello"
git push origin feature/feature-hello # 推送 Feature 分支
# 在 GitLab 建立 Merge Request (MR) → `feature/feature-login` → `develop`
完成后,取新版develop并删除Feature分支
git checkout develop # 切回 develop
git pull origin develop # 确保 develop 是最新的
git branch -d feature/feature-login # 删除本机 feature 分支
git push origin --delete feature/feature-login # 删除远端 feature 分支
释出Release
git checkout develop
git pull origin develop # 确保 develop 是最新的
git flow release start 1.0.0 # 建立 release/1.0.0 分支
git push -u origin release/1.0.0 # 推送到远端
修正 Release 分支的 Bug
git add .
git commit -m "[RELEASE] Fix minor bugs before release"
git push origin release/1.0.0
在 GitLab 建立 Merge Request
在 GitLab 建立 MR:
- 从
release/1.0.0
→main
- 标注「準备发布」
- 进行 Code Review
- CI/CD 可以自动部署到 Staging 或 Production
合併 Release 分支
在 GitLab 通过 Review 并合併 release/1.0.0
→ main
。
接下来,我们还需要把 release/1.0.0
的变更同步回 develop
,以确保 develop
也包含所有修正:
git checkout develop
git pull origin develop # 确保 develop 是最新的
git merge --no-ff release/1.0.0 # 把 Release 变更合併到 develop
git push origin develop # 推送最新的 develop
建立 Release Tag
git checkout main
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0 # 推送 Tag
删除 Release 分支
# 删除本机分支git
git branch -d release/1.0.0
# 删除远端分支
git push origin --delete release/1.0.0
修bugt状况
git checkout main
git pull origin main
git flow hotfix start hotfix-1.0.1
git push -u origin hotfix/hotfix-1.0.1
# 修正好bug
git add .
git commit -m "[HOTFIX] 修正生产环境错误"
git push origin hotfix/hotfix-1.0.1
建立 MR,请求 Code Review
- 在 GitLab 建立 Merge Request (MR)
hotfix/hotfix-1.0.1
→main
- 进行 Code Review,通过后合併
- CI/CD 立即部署到 Production
# 同步develop
git checkout develop
git pull origin develop
git merge --no-ff hotfix/hotfix-1.0.1
git push origin develop
# 建立Hotfix Tag
git checkout main
git tag -a v1.0.1 -m "Hotfix version 1.0.1"
git push origin v1.0.1
# 删除hotfix
git branch -d hotfix/hotfix-1.0.1
git push origin --delete hotfix/hotfix-1.0.1
以上是透过HelloWorld的程式来回温整个GitFlow流程。
元哥的笔记