使用git-flow自动化你的git分支工作流

Using git-flow to automate your git branching workflow

Posted by Raymond on August 15, 2017

这篇文章译自Using git-flow to automate your git branching workflow

Vincent Driessen的分支模型是一个基于git的分支管理和版本发布的策略,帮助开发人员在更大的软件项目中跟踪软件的features(功能分支),hotfixes(debug分支)和releases(发布的版本)。虽然这个工作流需要输入和记住很多命令,但是可以使用基于git命令的git-flow库来自动化流程的某些部分,使其更容易使用。

git-flow分支模型

安装git-flow (apt install git-flow)后, 你可以在你的git仓库中使用git-flow的init命令。你可以在已有的仓库钟使用git-flow, 但是让我们从一个新的仓库开始吧:

$ git flow init
Initialized 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,你会发现你从master分支切换到名为develop的新分支。

$ git branch
* develop
  master

develop分支作为开发过程中经常使用的默认分支, master分支则存放可以随时部署到生产环境的代码。所以从现在开始不要将代码推送到master分支,你应该用git push origin develop推送新的代码到仓库里。

Feature branches(功能分支)

git-flow可以通过使用feature branches來轻松地同时处理多个功能。 试一下, 用feature start加上新的分支名称 (比如, “authentication”):

$ git flow feature start authentication
Switched to a new branch 'feature/authentication'

Summary of actions:
- A new branch 'feature/authentication' was created, based on 'develop'
- You are now on branch 'feature/authentication'

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

     git flow feature finish authentication

打印信息说, 你现在在一个新的分支上,你可以在分支上实现你的功能。 在开发过程中使用通常的git命令即可,当功能开发完成时,使用feature finish来结束功能分支:

$ git flow feature finish authentication
Switched to branch 'develop'
Updating 9060376..00bafe4
Fast-forward
 authentication.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 authentication.txt
Deleted 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'

你的功能分支会合并到develop分支,并且当前分支会切换回develop分支。 在合并的过程中, git-flow使用git merge --no-ff feature/authentication在你的功能分支被删除之前确保不会丢失任何历史信息。

Versioned releases(版本发布分支)

如果你需要发布版本,你可以将准备部署到生产的代码用git-flow来新建新的release分支。

就像git-flow的其他分支一样,你也可以不使用release分支。或许更喜欢手动地用git merge --no-ff develop合并到master分支,这样做也没问题。然而,如果你在开发需要版本化的API或library,release分支可能会非常好用,其效果如你所愿:

$ 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'

当确定版本号并且在release分支做好一切版本发布的准备后,建议不要再做任何的临时修改,如果做了,git-flow会确认正确后再合并到masterdevelop,然后结束release分支:

$ git flow release finish 0.1.0
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 authentication.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 authentication.txt
Deleted 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

好了。 git-flow从origin拉取最新更改,再合并release分支到master,创建release标签并将release分支合并回develop,最后将release分支删除。

你目前处于master分支。git-flow确保了更改的内容都更新到master。你可以部署master后再切换回develop

Hotfixing production code(紧急修复分支)

因为你确保你的master分支的代码总是和生产的代码同步,所以你可以快速地修复在生产上发现的问题。

例如,你的assets没有在生产正常加载,你将回滚部署并启动紧急修复分支:

$ git flow hotfix start assets
Switched 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分支,除了前者基于master分支,后者基于develop分支。你将自动切换到新的hotfix分支,开始修复问题并发布次要版本号。 当你完成后,执行hotfix finish:

 $ git flow hotfix finish assets
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 assets.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 assets.txt
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
 assets.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 assets.txt
Deleted 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分支会合并到masterdevelop,创建新标签和删除hotfix分支。

为何不尝试git-flow?

当然,如果你没有做版本控制, Vincent的git的工作流和git-flow库可能不适合你。但是, 如果你在开发基于语义化版本的项目, 像Rubygem或者版本化的API, git-flow提供了几个简单的命令,这些命令封装了很多流程化的工作,使管理功能、推送新版本和修复bug变得很容易。Well, at leat on the git side.