yangkean / blog

official blog
5 stars 0 forks source link

谈谈 git-flow #2

Open yangkean opened 6 years ago

yangkean commented 6 years ago

There's only one rule: anything in the master branch is always deployable. -- Github

What & Why

一个人开发程序久了,习惯性地就会直接提交到 master 分支。但当项目逐渐大型且团队合作开发的人数逐渐变多时,才发现有必要规范下整个代码版本控制的流程,否则,混乱将是不可避免的,而 git-flow,就是这样一个规范代码版本控制的工作流程 (workflow)。git-flow 并没有改变基本的 git 命令,它只是通过脚本的方式将标准的 git 命令组合起来,配以清晰的引导提示和描述,让你通过几个简单的命令就可以完成一些麻烦的操作。

How to use it

下面我们来安装它吧:

$ brew install git-flow-avh

其它的安装方式或不同平台上的安装方法请参见 Installing git-flow

现在我们可以用 git-flow 来规范我们的版本控制了,cd 进入到你的项目,然后初始化它:

$ git flow init
Initialized empty Git repository in /Users/yangshao/Desktop/test/.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/]
Bugfix branches? [bugfix/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
Hooks and filters directory? [/Users/yangshao/Desktop/test/.git/hooks]

配置推荐使用默认的,所以一路回车即可。另外,如果你的项目不是一个 git 仓库,init 命令会帮你初始化的。

项目会有两个在项目的整个生命周期中长生不死的分支:

按照国际惯例,master 只能放产品代码,master 不能直接提交代码,代码提交要放到短命的分支上。develop 是进行任何新的开发的基础分支,功能分支的代码都是要合并到这个分支的,这个分支汇集所有已经完成的功能,并等待被整合到 master 分支中。

了解下那些活不久的分支

功能分支 (feature branch)

该分支用于新功能的开发。

开启一个分支:

$ git flow feature start rss-feed
Switched to a new branch 'feature/rss-feed'

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

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

     git flow feature finish rss-feed

完成该功能的开发时:

$ git flow feature finish rss-feed
Switched to branch 'develop'
Already up-to-date.
Deleted branch feature/rss-feed (was b8dfaaa).

Summary of actions:
- The feature branch 'feature/rss-feed' was merged into 'develop'
- Feature branch 'feature/rss-feed' has been locally deleted
- You are now on branch 'develop'

finish 该分支时该分支会被自动合并到 develop 分支。

预发布分支 (release branch)

当当前在 develop 分支的代码已经是个成熟的 release 版本时,即包含所有新的功能和必要的修复且被彻底地测试过了,这时候就可以生成一个 release 分支。

$ git flow release start 1.1.5
Switched to a new branch 'release/1.1.5'

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

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.1.5'

然后做最后的版本号更新等编辑工作即可完成该分支:

$ git flow release finish 1.1.5
Switched to branch 'master'
Deleted branch release/1.1.5 (was b8dfaaa).
1.1.6

Summary of actions:
- Release branch 'release/1.1.5' has been merged into 'master'
- The release was tagged '1.1.5'
- Release branch 'release/1.1.5' has been locally deleted
- You are now on branch 'master'

finish 该分支时该分支会被自动合并到 master 分支。

补丁分支 (hotfix branch) master

在已发布版本发现 bug 时使用该分支。

就语义来说,bugfix 是修复开发和测试时发现的 bug,hotfix 是修复当前已发布版本发现的 bug,前者用的比较少。

$ git flow hotfix start missing-link
Switched to a new branch 'hotfix/missing-link'

Summary of actions:
- A new branch 'hotfix/missing-link' was created, based on 'master'
- You are now on branch 'hotfix/missing-link'

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

     git flow hotfix finish 'missing-link'
$ git flow hotfix finish missing-link
Switched to branch 'develop'
Deleted branch hotfix/missing-link (was b8dfaaa).

Summary of actions:
- Hotfix branch 'hotfix/missing-link' has been merged into 'master'
- The hotfix was tagged 'missing-link'
- Hotfix branch 'hotfix/missing-link' has been locally deleted
- You are now on branch 'develop'

finish 该分支时该分支会被自动合并到 master 分支。

结语

当然,除了 git-flow,还有 github-flow、gitlab-flow,它们各有优缺点,但不管怎样,都是为了提高团队开发时代码管理的高效性。

Reference