GitLab Flow
核心概念:
- 主線開發 (
master
) → 只開發「已確認」的功能 - 測試環境 (
pre-production
) → 驗證沒問題才能進入正式環境 - 正式環境 (
production
) → 穩定運行,所有變更都經過測試
簡單來說:
- 寫程式 →
master
- 測試確認 →
pre-production
- 正式上線 →
production
1. 下載最新 master
git checkout master
git pull origin master
確保你的程式碼是最新的!
2. 開發新功能 (feature-*
)
git checkout -b feature-login
git push -u origin feature-login
開發專屬自己的功能分支!
3. 撰寫程式、提交變更
git add .
git commit -m "feat: 新增登入功能"
git push origin feature-login
開發完成後,推送程式碼!
4. 建立 Merge Request(MR)
- 打開 GitLab,建立 MR (feature-login → master)
- 請求 Code Review(其他人幫忙看程式碼)
- 通過 Review 後,合併到
master
5. 合併功能分支
git checkout master
git pull origin master # 確保 master 最新
git merge feature-login
git push origin master
合併後,刪除已完成的 feature
分支
git branch -d feature-login
git push origin --delete feature-login
6. 測試環境(Pre-Production)
git checkout pre-production
git merge master
git push origin pre-production
合併後,進行最後的測試!
這時 CI/CD 可以自動部署到 pre-production
7. 正式上線(Production)
git checkout production
git merge pre-production
git push origin production
所有測試通過後,才可以併入 production
,正式上線!
緊急修正(Hotfix)
如果 正式環境 (production
) 發生 Bug:
git checkout production
git checkout -b fix-login-bug
修復 Bug 後:
git add .
git commit -m "fix: 修正登入錯誤"
git push origin fix-login-bug
建立 MR → 合併 fix-login-bug
到 production
最後,把 production
的修正 同步回 pre-production
和 master
:
git checkout pre-production
git merge production
git push origin pre-production
git checkout master
git merge pre-production
git push origin master
# 刪除修正分支:
git branch -d fix-login-bug
git push origin --delete fix-login-bug
這樣確保 production
、pre-production
和 master
都同步最新修正!
最終總結
動作 | 分支 | 步驟 |
---|---|---|
開發 | feature-* | 建立分支 → 開發功能 → 提交 MR |
測試 | pre-production | master 合併 → 測試驗證 |
上線 | production | pre-production 確認後合併 |
修 Bug | fix-* | 直接從 production 修正,然後同步回 master |
這樣做的好處
- 比 GitFlow 簡單,不需要
develop
分支 - 適合 CI/CD,確保
production
只包含測試通過的版本 - 避免 Merge 過多分支,讓團隊流程順暢
- 修 Bug 直接從
production
修,確保穩定性
元哥的筆記