git就不用多說了分衫,對(duì)于版本控制有著很多優(yōu)點(diǎn)瘪菌,但是在多人開發(fā)的環(huán)境中名党,git flow制定了一個(gè)比較規(guī)范的分支管理和版本發(fā)布方案。
工作流程
git flow 是基于git的標(biāo)準(zhǔn)命令截酷,將git中的一些命令通過腳本組合了起來,實(shí)現(xiàn)了一個(gè)工作流程乾戏,其實(shí)如果你按照git flow的工作流程迂苛,不需要通過其他工具,也可以實(shí)現(xiàn)git flow的實(shí)現(xiàn)鼓择。但是使用了git flow腳本三幻,會(huì)讓這些操作來的更方便一些。
Git flow的工作分支主要為以下幾種:
- master:用于存放正式發(fā)布的版本惯退;
- develop:開發(fā)的主分支赌髓,一般由其他分支合并進(jìn)來;
- feature/*:新功能分支,一般情況下基于develop創(chuàng)建锁蠕,開發(fā)完成后合并到develop分支夷野;
- release/*:發(fā)布分支,基于develop最新版本創(chuàng)建的分支荣倾,用來測試悯搔,測試的bug會(huì)在這個(gè)分支上進(jìn)行修改。
- hotfix/*:線上版本修復(fù)bug的分支舌仍,基于master創(chuàng)建妒貌。
接下來會(huì)一個(gè)一個(gè)介紹這些分支。
安裝
git flow avh
正常通過homebrew就可以安裝git flow了铸豁。
brew install git-flow-avh
當(dāng)然還有其他的安裝方式灌曙,可以點(diǎn)擊這里 查看(MacOS),如果其他系統(tǒng)也可以在git flow avh的gitHub上查看节芥。
SourceTree
另外如果使用了SourceTree在刺,也可以直接通過SourceTree來進(jìn)行g(shù)it flow的管理。
右鍵點(diǎn)擊SourceTree頂部工具欄头镊,點(diǎn)擊自定義工具欄蚣驼。
直接將Git 工作流拖入到工具欄中就可以直接使用了。
在項(xiàng)目中添加git flow
在終端中相艇,cd到項(xiàng)目目錄下初始化就可以了颖杏。這個(gè)初始化和git本身的初始化關(guān)聯(lián)不大。
git flow init
跑了之后坛芽,會(huì)跟你確認(rèn)一些名稱留储,這里建議都使用默認(rèn)的名稱,一般情況下直接回車就可以了靡馁。
? GitFlowDemo git:(master) git flow init
Which branch should be used for bringing forth production releases?
- master
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? []
Hooks and filters directory? [/Users/zhongrui/Desktop/GitFlowDemo/.git/hooks]
? GitFlowDemo git:(develop)
或者在SourceTree中點(diǎn)擊Git工作流欲鹏,第一次點(diǎn)擊的時(shí)候會(huì)出現(xiàn)一些git flow初始化的內(nèi)容,同樣也是確認(rèn)一些名稱臭墨,只不過是用GUI表示出來了赔嚎。
操作成功之后,git flow的安裝就完成了胧弛,接下來可以愉快的使用它了尤误。
使用
在SourceTree中的使用比較簡單,直接通過需求點(diǎn)擊就可以了结缚,這里就不多做介紹损晤。主要說一下在Treminal中的使用。
添加新需求
添加新需求分別分為:
- 創(chuàng)建新需求
- 提交
- 發(fā)布新需求
- 完成新需求
接下來可以一個(gè)一個(gè)來看红竭。
創(chuàng)建新需求通過輸入命令
git flow feature start test_feature
其中test_feature為新需求的分支名稱尤勋,回車之后喘落,git flow 會(huì)自動(dòng)幫我們生成一個(gè)新的分支,并且切換到他下邊最冰。
? GitFlowDemo git:(develop) git flow feature start test_feature
Switched to a new branch 'feature/test_feature'
Summary of actions:
- A new branch 'feature/test_feature' was created, based on 'develop'
- You are now on branch 'feature/test_feature'
Now, start committing on your feature. When done, use:
git flow feature finish test_feature
? GitFlowDemo git:(feature/test_feature)
接下來我們就可以在這個(gè)分支下干活了
??????
??????
??????
干完活之后瘦棋,和平常一樣提交,add暖哨、commit赌朋。
? GitFlowDemo git:(feature/test_feature) ? git add .
? GitFlowDemo git:(feature/test_feature) ? git commit -m "test"
[feature/test_feature fb484b7] test
1 file changed, 4 insertions(+), 1 deletion(-)
? GitFlowDemo git:(feature/test_feature)
接下來如果需要push到遠(yuǎn)程,可以使用git的push篇裁,也可以使用
git flow feature publish test_feature
來發(fā)布這一個(gè)版本的新需求沛慢。
最后,當(dāng)我們這個(gè)需求開發(fā)完畢了达布。使用
git flow feature finish test_feature
Git flow 就會(huì)自動(dòng)幫我們刪除這個(gè)分支团甲,然后切換到develop分支去。
? GitFlowDemo git:(feature/test_feature) git flow feature finish test_feature
Switched to branch 'develop'
Updating 45b8dcf..fb484b7
Fast-forward
GitFlowDemo/ViewController.m | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Deleted branch feature/test_feature (was fb484b7).
Summary of actions:
- The feature branch 'feature/test_feature' was merged into 'develop'
- Feature branch 'feature/test_feature' has been locally deleted
- You are now on branch 'develop'
? GitFlowDemo git:(develop)
發(fā)布新版本
當(dāng)我們的develop分支的代碼已經(jīng)是一個(gè)成熟的代碼了往枣,意味著可以將它發(fā)布了伐庭,這時(shí)候就可以走發(fā)布新版本的流程。
同樣的分冈,新版本發(fā)布也是走之前的流程。
- 創(chuàng)建發(fā)布版本
- 提交
- 同步到遠(yuǎn)程
- 完成新版本發(fā)布
可以使用發(fā)布命令
git flow release start 1.0.0
其中1.0.0是我們這個(gè)版本的版本號(hào)霸株。
? GitFlowDemo git:(develop) git flow release start 1.0.0
Switched to a new branch 'release/1.0.0'
Summary of actions:
- A new branch 'release/1.0.0' was created, based on 'develop'
- You are now on branch 'release/1.0.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 '1.0.0'
? GitFlowDemo git:(release/1.0.0)
這時(shí)候git flow 會(huì)幫我們創(chuàng)建一個(gè)release分支雕沉。這時(shí)候如果有一些版本號(hào)之類的信息需要修改,在這個(gè)分支上修改之后去件,就可以準(zhǔn)備提交了坡椒。
同樣release也有publish版本,這些提交和發(fā)布與之前的feature基本一樣尤溜,都是將修改的代碼提交到遠(yuǎn)程倔叼,這里就不在贅述了。
直接進(jìn)到完成發(fā)布流程宫莱,完成發(fā)布需要使用
git flow release finish 1.0.0
運(yùn)行之后丈攒,首先git flow 會(huì)拉取遠(yuǎn)程倉庫,確保當(dāng)前的版本是最新版本授霸。
接下來release的內(nèi)容會(huì)被合并到master和develop兩個(gè)分支中去巡验,以保證新的功能部分也是最新的代碼。
并且release的代碼會(huì)被標(biāo)記上release碘耳,也就是tag显设。
接下來會(huì)刪除當(dāng)前的分支,并回到master辛辨。
? GitFlowDemo git:(release/1.0.0) git flow release finish 1.0.0
Switched to branch 'master'
Merge made by the 'recursive' strategy.
GitFlowDemo/ViewController.m | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Already on 'master'
fatal: no tag message?
Fatal: Tagging failed. Please run finish again to retry.
? GitFlowDemo git:(master)
修復(fù)bug(hotfix)
盡管在發(fā)布之前捕捂,我們已經(jīng)充分的對(duì)代碼進(jìn)行了測試瑟枫,但是誰也沒有辦法保證發(fā)布的新版本中一定沒有bug。如果遇到了bug指攒,這時(shí)候我們可以通過hotfix來進(jìn)行修復(fù)力奋。
Hotfix我們僅僅通過創(chuàng)建和完成兩個(gè)步驟就可以簡單的走完流程。
git flow hotfix start fix_bug
通過這個(gè)命令幽七,與之前一樣景殷,會(huì)創(chuàng)建一個(gè)fix_bug分支,并且切換到這個(gè)分支上來澡屡,但是稍有不同的是猿挚,這個(gè)分支不是來自于develop,而是基于master分支的驶鹉。
? GitFlowDemo git:(master) git flow hotfix start fix_bug
Switched to a new branch 'hotfix/fix_bug'
Summary of actions:
- A new branch 'hotfix/fix_bug' was created, based on 'master'
- You are now on branch 'hotfix/fix_bug'
Follow-up actions:
- Start committing your hot fixes
- Bump the version number now!
- When done, run:
git flow hotfix finish 'fix_bug'
? GitFlowDemo git:(hotfix/fix_bug)
接下來你就可以緊急修復(fù)你的bug 了绩蜻。
??????
修復(fù)完了之后,完成了測試室埋,就可以通過finish來完成這次bug修復(fù)办绝。
git flow hotfix finish fix_bug
接下來的過程和release比較相似,改動(dòng)的內(nèi)容會(huì)被合道m(xù)aster中姚淆,而且也會(huì)被合道develop中孕蝉。這樣就能保證在接下來的版本中不會(huì)出現(xiàn)同樣的錯(cuò)誤。
然后這個(gè)版本也將會(huì)被標(biāo)記下來腌逢。
之后就會(huì)刪除這個(gè)分支降淮。
常用命令
初始化
- git flow init:初始化一個(gè)現(xiàn)有的倉庫,添加git flow
feature
- git flow feature start [featureBranchName]:創(chuàng)建一個(gè)新功能分支
- git flow feature publish [featureBranchName]:推送新版本到遠(yuǎn)程服務(wù)器搏讶,也可以使用git push
- git flow feature finish [featureBranchName]:完成新功能開發(fā)佳鳖,會(huì)合并到develop分支,并刪除原分支
- git flow feature pull origin [featureBranchName]:拉取目標(biāo)分支的新功能媒惕,也可以使用git pull
- git flow feature track [featureBranchName]:跟蹤origin上的feature分支
release
- git flow release start [releaseBranchName]:創(chuàng)建一個(gè)準(zhǔn)備發(fā)布的分支
- git flow release publish [releaseBranchName]:創(chuàng)建release分支之后立即發(fā)布允許其他用戶向這個(gè)release分支提交內(nèi)容
- git flow release track [releaseBranchName]:簽出release版本的遠(yuǎn)程變更
- git flow release finish [releaseBranchName]:將release分支合并到master系吩,打上tag,將release分支合并到develop妒蔚,刪除原分支
hotfix
- git flow hotfix start [hotfixBranchName]:開始緊急修復(fù)穿挨,從master分出一個(gè)分支
- git flow hotfix finish [hotfixBranchName]:結(jié)束緊急修復(fù),代碼歸并到develop和master面睛,并且打上Tag絮蒿。
最后
以上就是對(duì)git flow的簡單使用,可以幫助我們做很多版本控制中很麻煩的問題叁鉴,以上內(nèi)容僅為個(gè)人學(xué)習(xí)記錄土涝,如果有什么不對(duì)的地方歡迎各位大佬評(píng)論指正。
參考文檔
Git 工作流程 - 阮一峰的網(wǎng)絡(luò)日志