使用 git-flow 自動化你的 git 工做流

介紹一下

git flow 分支模型相信你們或多或少都聽過,先放張圖鎮樓:html

git-flow.png

上面的圖看不懂不要緊(我也不懂==),今天講的是根據這個分支模型開發的 git-flow 命令行工具。只須要記住幾個簡單的命令,就能在工做中慢慢理解和應用這個分支模型~git

安裝 git-flow

咱們選擇比較流行的 avh 版本 gitflow-avhgithub

下面以 Mac OS X 爲例,安裝命令:bash

$ brew install git-flow-avh

初始化 Git 倉庫

下面針對一個只有 README.md 的文件夾執行如下命令,有條件的小夥伴能夠跟着操做一下,加深記憶。工具

$ git flow init
Initialized empty Git repository in /Users/savokiss/demos/gitflow/.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/]
Bugfix branches? [bugfix/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? [] v
Hooks and filters directory? [/Users/savokiss/demos/gitflow/.git/hooks]

能夠看到 git flow init 命令會要求你選擇兩個主分支,以及多個功能分支的前綴,咱們都使用默認值,而版本號 Tag 前綴使用 v測試

須要說明的是,git-flow 其實只是一系列 git 命令的組合,init 命令除了會新建分支,不會作其餘額外的操做。因此若是之後你再也不使用 git-flow,也不須要作任何變動。spa

注意上面的 init 操做完成,會自動幫咱們切到 develop 分支命令行

Feature 功能分支

假設咱們須要新建一個功能分支 auth 來作登陸功能的開發。讓咱們先切到 master 分支:翻譯

$ git checkout master

而後開始新的功能分支,控制檯輸出以下:code

$ git flow feature start auth
Switched to a new branch 'feature/auth'

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

Now, start committing on your feature. When done, use:

     git flow feature finish auth

能夠看到咱們是在 master 分支上執行的命令,可是 feature/auth 分支倒是基於 develop 切出的,由於根據上面的分支模型,全部的功能分支都應該從 develop 分支切出。這也就是 git-flow 的好處,你能夠不用在乎當前所在的分支,它會自動幫你保證沒有切錯分支~

接下來咱們來修改一下 README.md,添加一句話 登陸功能已經完成!,而後提交。

而後再執行完成命令,控制檯輸出以下:

$ git flow feature finish auth
Switched to branch 'develop'
Updating e69b22c..f7f48e2
Fast-forward
 # gitflow | 0
 README.md | 4 ++++
 2 files changed, 4 insertions(+)
 create mode 100644 # gitflow
 create mode 100644 README.md
Deleted branch feature/auth (was f7f48e2).

Summary of actions:
- The feature branch 'feature/auth' was merged into 'develop'
- Feature branch 'feature/auth' has been locally deleted
- You are now on branch 'develop'

從輸出能夠看出:

  1. feature/auth 分支被合併到了 develop 分支中。
  2. feature/auth 分支被刪除了
  3. 自動切換到了 develop 分支
而在 1 中 git-flow 內部使用以下命令 git merge --no-ff feature/auth 來進行合併,關於 --no-ff 參數,簡單來講,能夠更好地保留 feature 歷史記錄,感興趣的小夥伴能夠自行查閱相關資料哈

Release 版本發佈

一般狀況下,咱們會使用基於 semver 的語義化版本規範來管理軟件發佈。

git-flow 也支持建立 release 分支。下面咱們來發佈一個 0.1.0 版本:

$ 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'

能夠看出,該命令自動基於 develop 切出了 release/0.1.0 分支。
而後提示你在此分支上進行以下操做:

  1. 修改軟件版本號
  2. 進行鍼對此版本的 bug 修復

在正常的開發流程中,提測後的 bug 修復階段就能夠在這個 release/0.1.0 分支上作,而後等測試經過後,就能夠標記版本發佈完成:

$ git flow release finish 0.1.0
Switched to branch 'master'
Switched to branch 'develop'
Already up to date!
Merge made by the 'recursive' strategy.
Deleted branch release/0.1.0 (was f7f48e2).

Summary of actions:
- Release branch 'release/0.1.0' has been merged into 'master'
- The release was tagged 'v0.1.0'
- Release tag 'v0.1.0' has been back-merged into 'develop'
- Release branch 'release/0.1.0' has been locally deleted
- You are now on branch 'develop'

能夠看到,當你 finish 了一個 releasegit-flow 內部執行了如下操做:

  1. release/0.1.0 分支被合併到 masterdevelop
  2. v0.1.0 標籤被打在 master
  3. release/0.1.0 分支被刪除
  4. 當前切換到了 develop 分支

Hotfix 生產熱修復

因爲 master 分支一直反映了線上生產環境的情況,因此若是要進行生產熱修復,就須要從 master 分支切出,下面假如咱們線上的 banner 有一個 bug:

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

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

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

     git flow hotfix finish 'banner'

上面能夠看出,從 master 切出了一個 hotfix/banner 分支,
接下來你能夠在該分支進行生產 bug 修復的提交以及版本號的變動。

須要注意的是: hotfix 分支和 release 分支比較像,惟一的區別是 hotfix 分支是基於 master 切出的。

咱們先修改一下 README.md 文件並提交,而後完成 bug 修復:

$ git flow hotfix finish banner
Switched to branch 'develop'
Deleted branch hotfix/banner (was c6da343).

Summary of actions:
- Hotfix branch 'hotfix/banner' has been merged into 'master'
- The hotfix was tagged 'vbanner'
- Hotfix tag 'vbanner' has been back-merged into 'develop'
- Hotfix branch 'hotfix/banner' has been locally deleted
- You are now on branch 'develop'

上面完成了:

  1. hotfix/banner 分支被合併到 masterdevelop
  2. 這個熱更新被自動打上標籤 vbanner(因爲咱們前面設置標籤前綴爲 v)
  3. hotfix/banner 分支被刪除
  4. 目前切換到分支 develop
因爲我沒有關聯遠端 origin,因此這裏只會提示本地分支被刪除,若是關聯後遠端分支也會被刪除。

完了

上面咱們體驗了 git-flow 的基本操做流程。爲了方便演示,我是基於一個空項目操做的,實際上也能夠針對一個開發了好久的 git 倉庫來進行 git flow init,它會讓你選擇已經存在的分支做爲 生產分支開發分支 以及輸入相應的前綴,其他不會作任何多餘的操做。

另外 Sourcetree 是一個好用免費的 Git 圖形化客戶端,同時支持 WindowsMac,而且內置了 git-flow 工具,感興趣的小夥伴也能夠嘗試一下~

若是想了解更多,能夠參見文末的連接,其中有個 cheatsheet 也能夠快速熟悉 git flow 命令。

相信有了 git-flow 這個工具,在作分支管理的時候能夠相對無痛一點~

參考連接

歡迎關注個人公衆號:碼力全開(codingonfire)

每週更新一篇原創或翻譯文章~

codingonfire.jpg

相關文章
相關標籤/搜索