git flow
分支模型相信你們或多或少都聽過,先放張圖鎮樓:html
上面的圖看不懂不要緊(我也不懂==),今天講的是根據這個分支模型開發的 git-flow
命令行工具。只須要記住幾個簡單的命令,就能在工做中慢慢理解和應用這個分支模型~git
咱們選擇比較流行的 avh 版本 gitflow-avhgithub
下面以 Mac OS X 爲例,安裝命令:bash
$ brew install git-flow-avh
下面針對一個只有 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
分支命令行
假設咱們須要新建一個功能分支 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'
從輸出能夠看出:
feature/auth
分支被合併到了 develop
分支中。feature/auth
分支被刪除了develop
分支而在 1 中 git-flow 內部使用以下命令git merge --no-ff feature/auth
來進行合併,關於--no-ff
參數,簡單來講,能夠更好地保留 feature 歷史記錄,感興趣的小夥伴能夠自行查閱相關資料哈
一般狀況下,咱們會使用基於 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
分支。
而後提示你在此分支上進行以下操做:
在正常的開發流程中,提測後的 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
了一個 release
,git-flow
內部執行了如下操做:
release/0.1.0
分支被合併到 master
和 develop
v0.1.0
標籤被打在 master
上release/0.1.0
分支被刪除develop
分支因爲 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'
上面完成了:
hotfix/banner
分支被合併到 master
和 develop
vbanner
(因爲咱們前面設置標籤前綴爲 v)hotfix/banner
分支被刪除develop
因爲我沒有關聯遠端
origin
,因此這裏只會提示本地分支被刪除,若是關聯後遠端分支也會被刪除。
上面咱們體驗了 git-flow
的基本操做流程。爲了方便演示,我是基於一個空項目操做的,實際上也能夠針對一個開發了好久的 git 倉庫來進行 git flow init
,它會讓你選擇已經存在的分支做爲 生產分支
和 開發分支
以及輸入相應的前綴,其他不會作任何多餘的操做。
另外 Sourcetree
是一個好用免費的 Git 圖形化客戶端,同時支持 Windows
和 Mac
,而且內置了 git-flow
工具,感興趣的小夥伴也能夠嘗試一下~
若是想了解更多,能夠參見文末的連接,其中有個 cheatsheet
也能夠快速熟悉 git flow 命令。
相信有了 git-flow
這個工具,在作分支管理的時候能夠相對無痛一點~
歡迎關注個人公衆號:碼力全開(codingonfire)
每週更新一篇原創或翻譯文章~