git flow使用

2019-12-02  本文已影响0人  张囧瑞

git就不用多说了,对于版本控制有着很多优点,但是在多人开发的环境中,git flow制定了一个比较规范的分支管理和版本发布方案。

1.png

工作流程

git flow 是基于git的标准命令,将git中的一些命令通过脚本组合了起来,实现了一个工作流程,其实如果你按照git flow的工作流程,不需要通过其他工具,也可以实现git flow的实现。但是使用了git flow脚本,会让这些操作来的更方便一些。

Git flow的工作分支主要为以下几种:

接下来会一个一个介绍这些分支。

安装

git flow avh

正常通过homebrew就可以安装git flow了。

brew install git-flow-avh

当然还有其他的安装方式,可以点击这里 查看(MacOS),如果其他系统也可以在git flow avh的gitHub上查看。

SourceTree

另外如果使用了SourceTree,也可以直接通过SourceTree来进行git flow的管理。

右键点击SourceTree顶部工具栏,点击自定义工具栏。

2.png

直接将Git 工作流拖入到工具栏中就可以直接使用了。

3.png

在项目中添加git flow

在终端中,cd到项目目录下初始化就可以了。这个初始化和git本身的初始化关联不大。

git flow init

跑了之后,会跟你确认一些名称,这里建议都使用默认的名称,一般情况下直接回车就可以了。

➜  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中点击Git工作流,第一次点击的时候会出现一些git flow初始化的内容,同样也是确认一些名称,只不过是用GUI表示出来了。

4.png

操作成功之后,git flow的安装就完成了,接下来可以愉快的使用它了。

使用

在SourceTree中的使用比较简单,直接通过需求点击就可以了,这里就不多做介绍。主要说一下在Treminal中的使用。

添加新需求

添加新需求分别分为:

接下来可以一个一个来看。

创建新需求通过输入命令

git flow feature start test_feature

其中test_feature为新需求的分支名称,回车之后,git flow 会自动帮我们生成一个新的分支,并且切换到他下边。

➜  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)

接下来我们就可以在这个分支下干活了

🧱🧱🧱
🧱🧱🧱
🧱🧱🧱

干完活之后,和平常一样提交,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到远程,可以使用git的push,也可以使用

git flow feature publish test_feature

来发布这一个版本的新需求。

最后,当我们这个需求开发完毕了。使用

git flow feature finish test_feature

Git flow 就会自动帮我们删除这个分支,然后切换到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)

发布新版本

当我们的develop分支的代码已经是一个成熟的代码了,意味着可以将它发布了,这时候就可以走发布新版本的流程。

同样的,新版本发布也是走之前的流程。

可以使用发布命令

git flow release start 1.0.0

其中1.0.0是我们这个版本的版本号。

➜  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)

这时候git flow 会帮我们创建一个release分支。这时候如果有一些版本号之类的信息需要修改,在这个分支上修改之后,就可以准备提交了。

同样release也有publish版本,这些提交和发布与之前的feature基本一样,都是将修改的代码提交到远程,这里就不在赘述了。

直接进到完成发布流程,完成发布需要使用

git flow release finish 1.0.0

运行之后,首先git flow 会拉取远程仓库,确保当前的版本是最新版本。
接下来release的内容会被合并到master和develop两个分支中去,以保证新的功能部分也是最新的代码。
并且release的代码会被标记上release,也就是tag。
接下来会删除当前的分支,并回到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)

修复bug(hotfix)

尽管在发布之前,我们已经充分的对代码进行了测试,但是谁也没有办法保证发布的新版本中一定没有bug。如果遇到了bug,这时候我们可以通过hotfix来进行修复。

Hotfix我们仅仅通过创建和完成两个步骤就可以简单的走完流程。

git flow hotfix start fix_bug

通过这个命令,与之前一样,会创建一个fix_bug分支,并且切换到这个分支上来,但是稍有不同的是,这个分支不是来自于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)

接下来你就可以紧急修复你的bug 了。

🧱🧱🧱

修复完了之后,完成了测试,就可以通过finish来完成这次bug修复。

git flow hotfix finish fix_bug

接下来的过程和release比较相似,改动的内容会被合道master中,而且也会被合道develop中。这样就能保证在接下来的版本中不会出现同样的错误。
然后这个版本也将会被标记下来。
之后就会删除这个分支。

常用命令

初始化

feature

release

hotfix

最后

以上就是对git flow的简单使用,可以帮助我们做很多版本控制中很麻烦的问题,以上内容仅为个人学习记录,如果有什么不对的地方欢迎各位大佬评论指正。

参考文档

GitHub - petervanderdoes/gitflow-avh: AVH Edition of the git extensions to provide high-level repository operations for Vincent Driessen’s branching model

git flow的使用 - 简书

git-flow 的工作流程

Git 工作流程 - 阮一峰的网络日志

Gitflow 使用最强指北 - 掘金

Git Flow 是什麼?為什麼需要這種東西? - 為你自己學 Git | 高見龍

Git Flow工作流总结 - 简书

上一篇下一篇

猜你喜欢

热点阅读