0%

[Git] Git Flow 開發流程

Git flow

Git Flow

當越來越多人一起開發一個專案時,如果沒有好的開發流程,每個人的開發習慣都不同,到後期會讓整個專案很混亂,所以有人提出了一套開發流程: A successful Git branching model,而後來也陸續有一些不錯的開發流程: GitHub flow, GitLab flow,這一篇主要是紀錄 Git flow 開發流程。

分支

分支分為 master, develop, feature, releasehotfix 這五種分支,各分支分別負責不同的功能。

主要分支

主要的分支是 masterdevelop,其他的分支大部分都會在完成後刪除。

Master

最新、穩定、隨時可以上線的版本,只能從其他分支合併過來,不可直接 commit 到這個分支。通常會在這個分支的 commit 加上版本號的 tag.

Develop

主要用來開發的分支,當要開發新功能時,會從這個分支切出 feature 分支,當 feature 分支完成後,再合併回 develop 分支。

次要分支

次要的分支則是 feature, releasehotfix.

Feature

主要是在新功能開發的時候,從 develop 分出來的,當完成後,會再合併回 develop 分支。

Release

為了發布而建立的分支,主要是做為上線前的最後測試。測試完後,release 會同時合併到 masterdevelop 分支,確保所有程式都是最新的狀態。

Hotfix

當已經發布的產品發生緊急的問題時,從 master 切出來的分支,當修復完成後,會合併回 master,同時也會合併回 develop 分支,以確保所有程式都是最新狀態。

開始使用 Git Flow 開發流程

Git flow GitHub 專案 中有 git flow 工具,或是也可以直接使用 git 來實現 Git Flow 開發流程,這兩個方式我們都會稍微紀錄一下~

安裝 Git Flow

如果想要使用 git flow 工具,需要先依照以下步驟來安裝。

我們的開發環境是在 Windows 10 上使用 VS code,所以接下來的安裝及使用方式都是以此環境為範例,其他環境的安裝方式可以參考 Git flow GitHub 專案 中的說明。

建議安裝 Cygwin , 在安裝時需選擇安裝 git, util-linux and wget tools.

完成安裝後,在 Cygwin shell 執行以下指令即可安裝 git flow:

1
$ wget -q -O - --no-check-certificate https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | bash

初始化

先初始化 git repo

1
$ git init

Initialize repository for git flow

1
$ git flow init -d  # -d: 使用所有預設值

依照提示設定即可完成初始設定。

建立分支

建立 Feature 分支

如果一個功能有很多人一起進行,可以開多個 feature/myFeature 分支,例如: feature/myFeature/user1, feature/myFeature/user2, 最後 merge 回 feature/myFeature, 整個功能完全完成後再 merge 回 develop.

使用 git flow

1
2
3
4
5
$ git flow feature                              # 顯示 feature 分支
$ git flow feature start <name> [<base>] # 建立新的 feature 分支

$ git flow feature publish <name> # push feature branch to remote repo.
$ git flow feature pull <remote> <name> # pull feature branch to remote repo.

For feature branches, the <base> arg must be a commit on develop.

使用 git

1
$ git checkout -b feature/myFeature develop

feature 分支的名稱通常會加上 prefix: feature/, base 則是 develop.

建立 Release 分支

使用 git flow

1
2
$ git flow release                              # 顯示 release 分支
$ git flow release start <name> [<base>] # 建立新的 release 分支

For release branches, the <base> arg must be a commit on develop.

使用 git

1
$ git checkout -b release/1.0.0 develop

release 分支的名稱通常會加上 prefix: release/, 後面通常是加上版本號, base 則是 develop.

建立 Hotfix 分支

使用 git flow

1
2
$ git flow hotfix                               # 顯示 hotfix 分支
$ git flow hotfix start <name> [<base>] # 建立新的 hotfix 分支

For hotfix branches, the <base> arg must be a commit on master.

使用 git

1
$ git checkout -b hotfix/1.0.1

hotfix 分支的名稱通常會加上 prefix: hotfix/, 後面通常是加上修改的版本號.

完成分支任務

最後,執行以下指令來完成分支任務:

使用 git flow

1
2
3
$ git flow feature finish <name>
$ git flow release finish <name>
$ git flow hotfix finish <name>

使用 git

合併分支時,releasehotfix 分支記得要合併到 masterdevelop,合併可以使用 mergerebase 指令:

merge
修改內容的歷史記錄會維持原狀,但是合併後的歷史紀錄會變得更複雜。

切換到要合併的主要分支

1
$ git checkout <main_branch>  # <main_branch>: 要合併到的主要分支 (develop or master)

Merge:

1
$ git merge --no-ff <name>    # <name>: 要合併的分支名稱, ex. feature/myFeature

--no-ff 不要做 fast-forward 的動作。 Fast-forward 是指當被 merge 對象 (ex. feature/myFeature) 是目前分支 (ex. develop) 的 child 時, merge 後會完全看不出來有 merge 過。

rebase
修改內容的歷史記錄會接在要合併的分支後面,合併後的歷史記錄會比較清楚簡單,但是比使用 merge 更容易發生衝突。
注意: 只能用在還沒有 push 的情況下。

切換到要 rebase 的次要分支

1
$ git checkout feature/myFeature

Rebase:

1
$ git pull --rebase origin <main_branch>  # <main_branch>: 要 rebase 到的主要分支 (develop or master)

如果有先把 feature/myFeature 分支 pull 到本地, 可以直接執行:

1
$ git rebase <main_branch>

當發生衝突的時候,需要去修改衝突的地方,修改好之後,如果要繼續 rebase 就加上 –continue,如果要取消 rebase 的話,則加上–abort.

1
2
3
$ git add file.txt        # file.txt 為發生衝突的檔案, 修改好之後再加入
$ git rebase --continue # 繼續 rebase
$ git rebase --abort # 取消 rebase

最後記得要刪除次要分支:

1
$ git branch -d <name>  # <name>: 已合併到主要分支的次要分支名稱, ex. feature/myFeature

參考資料