git flow开发流程及基本使用

总体流程图

总体流程图

分支描述

主要分支

master: 永远处在即将发布(production-ready)状态;

develop: 最新的开发状态;

辅助分支

feature: 开发新功能的分支, 基于 dev, 完成后 merge 回 develop;

release: 准备要发布版本的分支, 正式提测版本,用来修复 bug. 基于 develop, 完成后合并( merge) 回 develop 和 master;

hotfix: 修复 master 上的问题, 等不及 release 版本就必须马上上线. 基于 master, 完成后 merge 回 master 和 develop;

git-flow基本使用

安装

git-flow默认在安装git scm(https://git-scm.com/)后会自动带上,使用如下命令确保存在该工具:

git flow help

如果存在,git-bash控制台将输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
usage: git flow <subcommand>

Available subcommands are:
init Initialize a new git repo with support for the branching model.
feature Manage your feature branches.
bugfix Manage your bugfix branches.
release Manage your release branches.
hotfix Manage your hotfix branches.
support Manage your support branches.
version Shows version information.
config Manage your git-flow configuration.
log Show log deviating from base branch.

Try 'git flow <subcommand> help' for details.

初始化

git flow init

运行该命令会询问分支中命名规定问题,如果不需要修改则默认回车,注意一个项目只需要执行一次这个过程,不要重复执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
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? [D:/lp2/testflow/.git/hooks]

新版本特性开发

开始开发

比如现在需要开始开发v6.9.0版本,则使用如下命令:

git flow feature start v6.9.0

执行后输出如下:

1
2
3
4
5
6
7
8
9
Switched to a new branch 'featurev6.9.0'

Summary of actions:
- A new branch 'featurev6.9.0' was created, based on 'develop'
- You are now on branch 'featurev6.9.0'

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

git flow feature finish v6.9.0

这个命令会创建一个叫featurev6.9.0的分支,这个分支是基于develop分支创建的,并且会自动切换到featurev6.9.0分支中。

发布新版本特性到git

git flow feature publish v6.9.0

如果此时你是与同事合作开发该特性,你是第一个发布你的改动上去的,就会看到控制台输出中有如下:

1
[new branch]      feature/v6.9.0 -> feature/v6.9.0

接下来讲解一个完整增加文件后发布流程

1.在项目中增加一个“新建文本文档.txt”
2.添加到git待commit:git add .
3.commit:git commit -m "添加文件测试"
4.发布git flow feature publish v6.9.0

取得协同开发的新版本特性

当与同事协同开发时,有发布必然有获取同事发布的最新特性,即为取得一个新特性分支:

git flow feature pull v6.9.0

完成新版本特性的开发

当完成了一个新版本开发,比如前面提到的v6.9.0,则使用如下命令:

git flow feature finish v6.9.0

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Switched to branch 'develop'
Updating 95ea817..cc33cf1
Fast-forward
...6\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 "\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"
To http://120.25.145.133:3000/Android/testflow.git
- [deleted] feature/v6.9.0
Deleted branch feature/v6.9.0 (was cc33cf1).

Summary of actions:
- The feature branch 'feature/v6.9.0' was merged into 'develop'
- Feature branch 'feature/v6.9.0' has been locally deleted; it has been remotely deleted from 'origin'
- You are now on branch 'develop'

这个命令实际操作步骤如下:

  1. 合并featurev6.9.0分支到develop
  2. 删除这个分支
  3. 切换回develop分支

注意,完成分支开发切换回dev后,dev此刻不会自动同步到远程更新,所以需要手动操作来把develop更新到远程仓库:

git push origin develop

作为一个release版本提测

再次重温release版本的职责:准备要发布版本的分支, 正式提测版本,用来修复 bug. 基于 dev, 完成后合并( merge) 回 develop 和 master;

准备release版本

当feature版本开发完成,进入提测阶段,就开始准备release版本,比如v6.9.0的正式提测

git flow release start v6.9.0

此时控制台输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
Switched to a new branch 'release/v6.9.0'

Summary of actions:
- A new branch 'release/v6.9.0' was created, based on 'develop'
- You are now on branch 'release/v6.9.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 v6.9.0

同步fix后的release版本到git

当release版本在测试中发现问题时,在对问题进行修复后,此时应该同步最新代码上git,使用到以下命令:

(在使用此命令前应该先执行修改变更及添加提交信息等操作,可参考本文:新版本特性开发-发布新版本特性到git)

git flow release publish v6.9.0

此时控制台输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Switched to a new branch 'release/v6.9.0'

Summary of actions:
- A new branch 'release/v6.9.0' was created, based on 'develop'
- You are now on branch 'release/v6.9.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 'v6.9.0'

完成release版本

当fixbug过程完成后,即可准备发布新版本,此时命令为:

git flow release finish v6.9.0

此时需要键入合并的message信息及所要打的tag。

之后使用git命令发布tags。

git push origin --tags

发布后续操作

此后续操作针对Android技术线,iOS理论上可以适用。
在执行上述发布操作后,代码已经合并到master,此时切换到master分支进行apk文件打包工作,此时打出来的包作为应用市场发布的基础包。

紧急修复

开始一个版本的紧急修复

比如需要从v1.0.2版本中紧急修复,则先将master指向该版本的commit id(查看对应tag即可),假如该commit id为:8bfa4ec

则命令如下:

git checkout 8bfa4ec

此时代码已经指向该版本,此时使用命令:

git flow hotfix start v1.0.2-hotfix

此时控制台输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Previous HEAD position was 8bfa4ec Merge branch 'release/v1.0.2'
Switched to a new branch 'hotfix/v1.0.2-hotfix'

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

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

git flow hotfix finish 'v1.0.2-hotfix'

完成紧急修复

完成紧急修复后,使用如下命令:

git flow hotfix finish v1.0.2-hotfix

此时需要键入合并的message信息及所要打的tag。

之后使用git命令发布tags。

git push origin --tags

参考资料

Git Cheat Sheet 中文版
GitFlow

文章作者: Kevin Wu
文章链接: https://kevinwu.cn/p/84604d58/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 KevinWu的博客
支付宝打赏