[翻译文章] git-flow https://github.com/nvie/gitflow
的分支模型是一个 git 分支和发布管理规范,用来帮助开发人员在大型的软件项目中追踪features, hotfixes 和 releases.这个工作流有很多命令去输入和记住,所以还有一个 git 的子命令 git-flow 的库来做一部分的自动化流程,使得其更加方便使用.
在安装 (brew install git-flow)之后,你就可以再你的工程中用init
命令来使用 git-flow 了.你可以使用现有工程,但我们还是开启一个新的项目: $ git flow initInitialized empty Git repository in ~/project/.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/]Release branches? [release/]Hotfix branches? [hotfix/]Support branches? [support/]Version tag prefix? []复制代码
git-flow只是围绕现有git命令的一个包装,所以init
命令不会更改你的项目中的任何内容,除了为你创建分支。 如果你不想使用git-flow,则无需更改或删除,你只需停止使用git-flow命令。 如果你设置好之后运行git branch
, 你会发现你的主分支切换到了一个名为develop
的新分支.
$ git branch* develop master复制代码
develop
分支还是大部分工作在处理的默认分支,然后master
分支还是记录已经开发完成的代码.
Feature 分支
git-flow中的 feature 功能让多个并行开发的任务变得易于处理,要用这个功能,用 feature start
加上你的新功能的名字.(下面例子中的: authentication
)
$ git flow feature finish authenticationSwitched to branch 'develop'Updating 9060376..00bafe4Fast-forward authentication.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 authentication.txtDeleted branch feature/authentication (was 00bafe4).Summary of actions:- The feature branch 'feature/authentication' was merged into 'develop'- Feature branch 'feature/authentication' has been removed- You are now on branch 'develop'复制代码
你的feature
功能将会被合并,然后会回到develop
分支.在内部, git-flow 使用了git merge --no-ff feature/authentication
来确保你的 feature分支在被移除前有任务未提交的内容.
Versioned releases
当你准备将新版本部署到生产的时候,如果你需要发布和打 tag, 可以使用git flow 的 release 功能. 跟 git-flow 的其他功能一样,你不想用 release 功能也可以.喜欢手动git merge --no-ff develop
合并代码而且还没有 tag?没问题,但是,如果你正在使用版本化的 API 或者库的时候, release 功能可能非常有用,它的作用跟你希望的一样:
$ git flow release start 0.1.0Switched 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'复制代码
输入版本号,并在 release 分支中执行发布项目所需的一切.我个人不愿意在 release 分支上做任何改动或者bug fix,但如果你做了, git-flow 也会保证你做的任何改动都会合并到master
和develop
分支.然后, 完成这次 release:
$ git flow release finish 0.1.0Switched to branch 'master'Merge made by the 'recursive' strategy. authentication.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 authentication.txtDeleted branch release/0.1.0 (was 1b26f7c).Summary of actions:- Latest objects have been fetched from 'origin'- Release branch has been merged into 'master'- The release was tagged '0.1.0'- Release branch has been back-merged into 'develop'- Release branch 'release/0.1.0' has been deleted复制代码
Boom! git-flow 从远端拉取,把release
合并到master
,根据版本号打上 tag.而且在移除 release 分支之前,反向合并了 release 分支中的提交到develop
分支. 在这个命令之后,你还是master
分支,在你回到develop
之前你可以部署这次发布,git-flow 的作用是为了把release
的改动更新到master
上.
Hotfix
当线上发生 bug 的时候,因为你的master
分支与线上环境保持一致,所以你可以很快的发现线上环境的任何问题. 比如,生产环境发现有图片没有加载出来,你会回滚部署然后开启一个hotfix
分支:
$ git flow hotfix start assetsSwitched to a new branch 'hotfix/assets'Summary of actions:- A new branch 'hotfix/assets' was created, based on 'master'- You are now on branch 'hotfix/assets'Follow-up actions:- Bump the version number now!- Start committing your hot fixes- When done, run: git flow hotfix finish 'assets'复制代码
hotfix
分支和release
分支大部分都是一样的,除了hotfix
是基于master
而release
是基于develop
.你可以很快的切换到hotfix
分支然后开始修复问题,然后修改 minor 版本号.当你完成之后,输入hotfix finish
:
$ git flow hotfix finish assetsSwitched to branch 'master'Merge made by the 'recursive' strategy. assets.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 assets.txtSwitched to branch 'develop'Merge made by the 'recursive' strategy. assets.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 assets.txtDeleted branch hotfix/assets (was 08edb94).Summary of actions:- Latest objects have been fetched from 'origin'- Hotfix branch has been merged into 'master'- The hotfix was tagged '0.1.1'- Hotfix branch has been back-merged into 'develop'- Hotfix branch 'hotfix/assets' has been deleted复制代码
跟完成一个release
分支一样,hotfix
分支会被合并到master
和develop
分支,在release
分支打个 tag 之后hotfix
分支会被移除.
你为什么不用git-flow?
如果你没有版本控制发布,这套 git 的工作流程和 git-flow 库可能不适合你.但是,如果你是有一个工程,就像 Rubygem 或者有版本API库,git-flow将为您提供几个简单的命令,这些命令将在底层做了进行大量工作,从而实现功能的开发,发布版本和修复bug更容易.