[譯] 用 git flow 來讓 git workflow 自動化

一銘發表於2017-12-14

[翻譯文章] 原文連結 git-flow https://github.com/nvie/gitflow

Vincent Driessen的分支模型是一個 git 分支和釋出管理規範,用來幫助開發人員在大型的軟體專案中追蹤features, hotfixes 和 releases.這個工作流有很多命令去輸入和記住,所以還有一個 git 的子命令 git-flow 的庫來做一部分的自動化流程,使得其更加方便使用.

git-flow
在安裝 git-flow (brew install git-flow)之後,你就可以再你的工程中用init命令來使用 git-flow 了.你可以使用現有工程,但我們還是開啟一個新的專案:

$ git flow init
Initialized empty Git repository in ~/project/.git/
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
複製程式碼

git-flow只是圍繞現有git命令的一個包裝,所以init命令不會更改你的專案中的任何內容,除了為你建立分支。 如果你不想使用git-flow,則無需更改或刪除,你只需停止使用git-flow命令。 如果你設定好之後執行git branch, 你會發現你的主分支切換到了一個名為develop的新分支.

$ git branch
* develop
  master
複製程式碼

develop分支還是大部分工作在處理的預設分支,然後master分支還是記錄已經開發完成的程式碼.

Feature 分支

git-flow中的 feature 功能讓多個並行開發的任務變得易於處理,要用這個功能,用 feature start加上你的新功能的名字.(下面例子中的: authentication)

$ git flow feature finish authentication
Switched to branch 'develop'
Updating 9060376..00bafe4
Fast-forward
 authentication.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 authentication.txt
Deleted branch feature/authentication (was 00bafe4).

Summary of actions:
- The feature branch 'feature/authentication' was merged into 'develop'
- Feature branch 'feature/authentication' has been removed
- You are now on branch 'develop'
複製程式碼

你的feature 功能將會被合併,然後會回到develop分支.在內部, git-flow 使用了git merge --no-ff feature/authentication 來確保你的 feature分支在被移除前有任務未提交的內容.

Versioned releases

當你準備將新版本部署到生產的時候,如果你需要釋出和打 tag, 可以使用git flow 的 release 功能. 跟 git-flow 的其他功能一樣,你不想用 release 功能也可以.喜歡手動git merge --no-ff develop合併程式碼而且還沒有 tag?沒問題,但是,如果你正在使用版本化的 API 或者庫的時候, release 功能可能非常有用,它的作用跟你希望的一樣:

$ git flow release start 0.1.0
Switched to a new branch 'release/0.1.0'

Summary of actions:
- A new branch 'release/0.1.0' was created, based on 'develop'
- You are now on branch 'release/0.1.0'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:
    
 git flow release finish '0.1.0'
複製程式碼

輸入版本號,並在 release 分支中執行釋出專案所需的一切.我個人不願意在 release 分支上做任何改動或者bug fix,但如果你做了, git-flow 也會保證你做的任何改動都會合併到masterdevelop分支.然後, 完成這次 release:

$ git flow release finish 0.1.0
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 authentication.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 authentication.txt
Deleted branch release/0.1.0 (was 1b26f7c).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Release branch has been merged into 'master'
- The release was tagged '0.1.0'
- Release branch has been back-merged into 'develop'
- Release branch 'release/0.1.0' has been deleted
複製程式碼

Boom! git-flow 從遠端拉取,把release合併到master,根據版本號打上 tag.而且在移除 release 分支之前,反向合併了 release 分支中的提交到develop分支. 在這個命令之後,你還是master分支,在你回到develop之前你可以部署這次釋出,git-flow 的作用是為了把release的改動更新到master上.

Hotfix

當線上發生 bug 的時候,因為你的master分支與線上環境保持一致,所以你可以很快的發現線上環境的任何問題. 比如,生產環境發現有圖片沒有載入出來,你會回滾部署然後開啟一個hotfix分支:

$ git flow hotfix start assets
Switched to a new branch 'hotfix/assets'

Summary of actions:
- A new branch 'hotfix/assets' was created, based on 'master'
- You are now on branch 'hotfix/assets'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

     git flow hotfix finish 'assets'
複製程式碼

hotfix分支和release分支大部分都是一樣的,除了hotfix是基於masterrelease是基於develop.你可以很快的切換到hotfix分支然後開始修復問題,然後修改 minor 版本號.當你完成之後,輸入hotfix finish:

$ git flow hotfix finish assets
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 assets.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 assets.txt
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
 assets.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 assets.txt
Deleted branch hotfix/assets (was 08edb94).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'master'
- The hotfix was tagged '0.1.1'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/assets' has been deleted
複製程式碼

跟完成一個release分支一樣,hotfix分支會被合併到masterdevelop分支,在release分支打個 tag 之後hotfix分支會被移除.

你為什麼不用git-flow?

如果你沒有版本控制釋出,這套 git 的工作流程和 git-flow 庫可能不適合你.但是,如果你是有一個語義化的版本工程,就像 Rubygem 或者有版本API庫,git-flow將為您提供幾個簡單的命令,這些命令將在底層做了進行大量工作,從而實現功能的開發,釋出版本和修復bug更容易.

相關文章