本文,转于老外的一个实际git的管理项目和想法。这篇文章作者把自己工作中实际使用git的版本控制构建做了总结。对的,就是git 的版本管理的构架。文章列举了一个复杂的项目开发生命维护的框架,构建了一个git版本控制的模型。有非常有意义的参考。

原文地址如下:http://nvie.com/posts/a-successful-git-branching-model/

作者:By Vincent Driessen

GIT 项目分支策略和释放管理

In this post I present the development model that I’ve introduced for some of my projects (both at work and private) about a year ago, and which has turned out to be very successful. I’ve been meaning to write about it for a while now, but I’ve never really found the time to do so thoroughly, until now. I won’t talk about any of the projects’ details, merely about the branching strategy and release management.

It focuses around Git as the tool for the versioning of all of our source code. (By the way, if you’re interested in Git, our company GitPrime provides some awesome realtime data analytics on software engineering performance.)

工具选择 Why git?

For a thorough discussion on the pros and cons of Git compared to centralized source code control systems, see the web. There are plenty of flame wars going on there. As a developer, I prefer Git above all other tools around today. Git really changed the way developers think of merging and branching. From the classic CVS/Subversion world I came from, merging/branching has always been considered a bit scary 可怕的 (“beware of merge conflicts, they bite you!”) and something you only do every once in a while.

But with Git, these actions are extremely cheap and simple, and they are considered one of the core parts of your daily workflow, really. For example, in CVS/Subversion books, branching and merging is first discussed in the later chapters (for advanced users), while in every Git book, it’s already covered in chapter 3 (basics).

As a consequence of its simplicity and repetitive nature, branching and merging are no longer something to be afraid of. Version control tools are supposed to assist in branching/merging more than anything else.

Enough about the tools, let’s head onto the development model. The model that I’m going to present here is essentially no more than a set of procedures that every team member has to follow in order to come to a managed software development process.

没有中心的中心 Decentralized but centralized

The repository setup that we use and that works well with this branching model, is that with a central “truth” repo. Note that this repo is only considered to be the central one (since Git is a DVCS, there is no such thing as a central repo at a technical level). We will refer to this repo as origin, since this name is familiar to all Git users.

Each developer pulls and pushes to origin. But besides the centralized push-pull relationships, each developer may also pull changes from other peers to form sub teams. For example, this might be useful to work together with two or more developers on a big new feature, before pushing the work in progress to origin prematurely. In the figure above, there are subteams of Alice and Bob, Alice and David, and Clair and David.

Technically, this means nothing more than that Alice has defined a Git remote, named bob, pointing to Bob’s repository, and vice versa.

主分支 The main branches

At the core, the development model is greatly inspired by existing models out there. The central repo holds two main branches with an infinite lifetime:

  • master
  • develop

The master branch at origin should be familiar to every Git user. Parallel to the master branch, another branch exists calleddevelop.

We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state.【这是一个重要的概念,主分支应该是一个总是为产品级别READY的状态,中间不应该有任何打断】

We consider origin/develop to be the main branch where the source code of HEAD always reflects a state with the latest delivered development changes for the next release. Some would call this the “integration branch”. This is where any automatic nightly builds are built from.

When the source code in the develop branch reaches a stable point and is ready to be released, all of the changes should be merged back into master somehow and then tagged with a release number. How this is done in detail will be discussed further on.

Therefore, each time when changes are merged back into master, this is a new production release by definition. We tend to be very strict at this, so that theoretically, we could use a Git hook script to automatically build and roll-out our software to our production servers everytime there was a commit on master.【master上的每一次merged都必须严格地能让自动编译工具编译成功,并且可以随时溯源】

支持分支 Supporting branches 【必须有有限的生存周期】

Next to the main branches master and develop, our development model uses a variety of supporting branches to aid parallel development between team members, ease tracking of features, prepare for production releases and to assist in quickly fixing live production problems. Unlike the main branches, these branches always have a limited life time, since they will be removed eventually.【最终将被移出】

The different types of branches we may use are:

  • Feature branches   功能开发分支
  • Release branches 代码发布分支
  • Hotfix branches 紧急修复分支

Each of these branches have a specific purpose and are bound to strict rules as to which branches may be their originating branch and which branches must be their merge targets. We will walk through them in a minute.【这些短生命周期的分支,必须严格遵循严格的标准定义 第一,他们是从哪里切出来的分支。 第二,他们将从哪里合并回去。】

By no means are these branches “special” from a technical perspective. The branch types are categorized by how we use them. They are of course plain old Git branches.

新功能实现分支 Feature branches

May branch off from:
develop
Must merge back into:
develop
Branch naming convention:
anything except masterdeveloprelease-*, or hotfix-*

Feature branches (or sometimes called topic branches) are used to develop new features for the upcoming or a distant future release. When starting development of a feature, the target release in which this feature will be incorporated may well be unknown at that point. The essence of a feature branch is that it exists as long as the feature is in development, but will eventually be merged back into develop (to definitely add the new feature to the upcoming release) or discarded (in case of a disappointing experiment).

Feature branches typically exist in developer repos only, not in origin.

Creating a feature branch

When starting work on a new feature, branch off from the develop branch.

$ git checkout -b myfeature develop
Switched to a new branch "myfeature"

Incorporating a finished feature on develop

Finished features may be merged into the develop branch to definitely add them to the upcoming release:

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff myfeature
Updating ea1b82a..05e9557
(Summary of changes)
$ git branch -d myfeature
Deleted branch myfeature (was 05e9557).
$ git push origin develop

The --no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward. This avoids losing information about the historical existence of a feature branch and groups together all commits that together added the feature. Compare:【这点非常有意思,为了保证记录必须如此选择】

In the latter case, it is impossible to see from the Git history which of the commit objects together have implemented a feature—you would have to manually read all the log messages. Reverting a whole feature (i.e. a group of commits), is a true headache in the latter situation, whereas it is easily done if the --no-ff flag was used.

Yes, it will create a few more (empty) commit objects, but the gain is much bigger than the cost.

版本发布分支  Release branches

May branch off from:
develop
Must merge back into:
develop and master
Branch naming convention:
release-*

Release branches support preparation of a new production release. They allow for last-minute dotting of i’s and crossing t’s. Furthermore, they allow for minor bug fixes and preparing meta-data for a release (version number, build dates, etc.). By doing all of this work on a release branch, thedevelop branch is cleared to receive features for the next big release.

The key moment to branch off a new release branch from develop is when develop (almost) reflects the desired state of the new release. At least all features that are targeted for the release-to-be-built must be merged in to develop at this point in time. All features targeted at future releases may not—they must wait until after the release branch is branched off.

It is exactly at the start of a release branch that the upcoming release gets assigned a version number—not any earlier. Up until that moment, the develop branch reflected changes for the “next release”, but it is unclear whether that “next release” will eventually become 0.3 or 1.0, until the release branch is started. That decision is made on the start of the release branch and is carried out by the project’s rules on version number bumping.

Creating a release branch

Release branches are created from the develop branch. For example, say version 1.1.5 is the current production release and we have a big release coming up. The state of develop is ready for the “next release” and we have decided that this will become version 1.2 (rather than 1.1.6 or 2.0). So we branch off and give the release branch a name reflecting the new version number:

$ git checkout -b release-1.2 develop
Switched to a new branch "release-1.2"
$ ./bump-version.sh 1.2
Files modified successfully, version bumped to 1.2.
$ git commit -a -m "Bumped version number to 1.2"
[release-1.2 74d9424] Bumped version number to 1.2
1 files changed, 1 insertions(+), 1 deletions(-)

After creating a new branch and switching to it, we bump the version number. Here, bump-version.sh is a fictional shell script that changes some files in the working copy to reflect the new version. (This can of course be a manual change—the point being that some files change.) Then, the bumped version number is committed.

This new branch may exist there for a while, until the release may be rolled out definitely. During that time, bug fixes may be applied in this branch (rather than on the develop branch). Adding large new features here is strictly prohibited. They must be merged into develop, and therefore, wait for the next big release.

Finishing a release branch

When the state of the release branch is ready to become a real release, some actions need to be carried out. First, the release branch is merged into master (since every commit on master is a new release by definition, remember). Next, that commit on master must be tagged for easy future reference to this historical version. Finally, the changes made on the release branch need to be merged back into develop, so that future releases also contain these bug fixes.

The first two steps in Git:

$ git checkout master
Switched to branch 'master'
$ git merge --no-ff release-1.2
Merge made by recursive.
(Summary of changes)
$ git tag -a 1.2

The release is now done, and tagged for future reference.

Edit: You might as well want to use the -s or -u <key> flags to sign your tag cryptographically.

To keep the changes made in the release branch, we need to merge those back into develop, though. In Git:

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff release-1.2
Merge made by recursive.
(Summary of changes)

This step may well lead to a merge conflict (probably even, since we have changed the version number). If so, fix it and commit.

Now we are really done and the release branch may be removed, since we don’t need it anymore:

$ git branch -d release-1.2
Deleted branch release-1.2 (was ff452fe).

Hotfix branches

May branch off from:
master
Must merge back into:
develop and master
Branch naming convention:
hotfix-*

Hotfix branches are very much like release branches in that they are also meant to prepare for a new production release, albeit unplanned. They arise from the necessity to act immediately upon an undesired state of a live production version. When a critical bug in a production version must be resolved immediately, a hotfix branch may be branched off from the corresponding tag on the master branch that marks the production version.

The essence is that work of team members (on thedevelop branch) can continue, while another person is preparing a quick production fix.

Creating the hotfix branch

Hotfix branches are created from the master branch. For example, say version 1.2 is the current production release running live and causing troubles due to a severe bug. But changes on developare yet unstable. We may then branch off a hotfix branch and start fixing the problem:

$ git checkout -b hotfix-1.2.1 master
Switched to a new branch "hotfix-1.2.1"
$ ./bump-version.sh 1.2.1
Files modified successfully, version bumped to 1.2.1.
$ git commit -a -m "Bumped version number to 1.2.1"
[hotfix-1.2.1 41e61bb] Bumped version number to 1.2.1
1 files changed, 1 insertions(+), 1 deletions(-)

Don’t forget to bump the version number after branching off!

Then, fix the bug and commit the fix in one or more separate commits.

$ git commit -m "Fixed severe production problem"
[hotfix-1.2.1 abbe5d6] Fixed severe production problem
5 files changed, 32 insertions(+), 17 deletions(-)

Finishing a hotfix branch

When finished, the bugfix needs to be merged back into master, but also needs to be merged back into develop, in order to safeguard that the bugfix is included in the next release as well. This is completely similar to how release branches are finished.

First, update master and tag the release.

$ git checkout master
Switched to branch 'master'
$ git merge --no-ff hotfix-1.2.1
Merge made by recursive.
(Summary of changes)
$ git tag -a 1.2.1

Edit: You might as well want to use the -s or -u <key> flags to sign your tag cryptographically.

Next, include the bugfix in develop, too:

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff hotfix-1.2.1
Merge made by recursive.
(Summary of changes)

The one exception to the rule here is that, when a release branch currently exists, the hotfix changes need to be merged into that release branch, instead of develop. Back-merging the bugfix into the release branch will eventually result in the bugfix being merged into develop too, when the release branch is finished. (If work in develop immediately requires this bugfix and cannot wait for the release branch to be finished, you may safely merge the bugfix into develop now already as well.)

Finally, remove the temporary branch:

$ git branch -d hotfix-1.2.1
Deleted branch hotfix-1.2.1 (was abbe5d6).

Summary

While there is nothing really shocking new to this branching model, the “big picture” figure that this post began with has turned out to be tremendously useful in our projects. It forms an elegant mental model that is easy to comprehend and allows team members to develop a shared understanding of the branching and releasing processes.

A high-quality PDF version of the figure is provided here. Go ahead and hang it on the wall for quick reference at any time.

Update: And for anyone who requested it: here’s the gitflow-model.src.key of the main diagram image (Apple Keynote).


Git-branching-model.pdf

If you want to get in touch, I'm @nvie on Twitter.

【版本控制管理】 深入 001 A successful Git branching model GIT 项目分支策略和释放管理相关推荐

  1. git-flow的分支管理 (Git branching model)

    master分支(下版开发, 开始测试后锁分支) 热修复分支(基于tag拉分支) 发布分支(暂不用,除非有多余的测试资源,并行测试) 开发分支(暂不用) 功能分支(长期的功能开发) master分支+ ...

  2. A successful Git branching model

    原文:http://nvie.com/posts/a-successful-git-branching-model/ In this post I present the development mo ...

  3. Git 技术篇 - Github在项目分支里下载某个文件方法,Github项目里的单个js文件下载实例演示

    有的小伙伴通过一些链接进入 github 的某个项目分支里后,发现不知道到怎么下载文件,下面来介绍一下. 单个文件下载方法: 点击查看文件. 在点进 Raw . 然后通过 ctrl + s 保存即可. ...

  4. learn git branching学习整理3

    介绍 learn git branching是一个非常好的git学习网站,它与传统的文字讲解相比较起来有一个非常大的亮点----图形化的git提交树可以实时的反馈并告诉你当前所做的git操作在对于代码 ...

  5. Git Flow分支策略与Azure DevOps相关功能简介

    想了很久,还是写这么一篇文章来总结一下有关分支策略和DevOps的一些内容吧.其实,DevOps相关的内容并不是我的工作范围,不过对于敏捷开发.DevOps.项目管理等等这一系列的与开发过程相关的内容 ...

  6. 持续交付之基于Git Flow代码分支策略实践

    文章目录 一.前言 二.主干开发(TBD) 三.特性开发 1.Git Flow 2.产品分支策略 2.1.基本情况 2.2.分支管理 2.3.分支合并时间 2.4.初始化配置 2.5.迭代开发 2.6 ...

  7. 浅议项目施工合同文书5s管理制度实施办法

    项目施工合同文书管理为何要应用5s管理制度?华天谋5s管理专家概述:5S管理在现场管理中已经得到普遍应用,其效率提升和时间节约也被广泛认同.施工项目合同文本是一种很重要的文件,经常需要查阅和调用,当数 ...

  8. GIT(一):版本控制、git历史、git基本概念、git安装配置、git版本管理(本地和远程)、git分支管理

    目录 一.版本控制 1.1 版本控制概念 1.2 版本控制软件分类 1.2.1 集中式版本控制 1.2.2 分布式版本控制 二.git历史 三.GIT基本概念 3.1 GIT原理 3.1.1 GIT基 ...

  9. 【代码管理】GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流

    GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details ...

最新文章

  1. 小团队如何玩转物联网开发?
  2. IBM一周内拥有10万Lotus Symphony用户
  3. vscode新建html,没有模板
  4. vs2017运行显示系统找不到指定的文件_windows 平台使用 VS2017 编译 libevent 源码
  5. Worksheet.get_Range Method
  6. oracle查大事务语句,查询Oracle 中死事务的语句
  7. 高级C语言教程-关键字和运算符
  8. PHP之factory
  9. oracle 如何查看当前用户的表空间名称
  10. linux shell指令 amp,shell入门基础amp;常见命令及用法
  11. matlab求递归问题,matlab利用递归求解差分方程
  12. 逐步认识C#四种判断相等的方法
  13. log4j.xml的实用例子
  14. 关于Oracle表访问方式的总结
  15. FusionChartsFree调用json数据的简单例子
  16. Linux小游戏——单机掷骰子
  17. 互联网营销,不同阶层的人脉关系,对你真的有用吗?
  18. sketch插件开发
  19. 「深度」规模仅为12亿元,充电桩建设真的是朝阳产业吗?
  20. JavaScript:实现AlphaNumericalSort字母数字排序算法(附完整源码)

热门文章

  1. java 递归 遍历目录下的所有文件
  2. svg 自己做动画图片 GSAP真的好用
  3. goldilocks数据库_如何找到您的开源Goldilocks区域
  4. python md5加密数据
  5. VS2103没有“dirent.h”文件
  6. 单进程服务器-select版
  7. 微信公众平台应用开发实战
  8. Android中文语音合成(TTS)各家引擎对比 .
  9. ReportViewer教程(15)-矩阵报表-1
  10. Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)