A successful git branching Model

Source: Internet
Author: User

In this post I present the development model that I 've introduced for all of myprojects (both at work and private) about a year ago, and which has turned outto be very successful. I 've been meaning to write about it for a while now, buti' ve
Never really found the time to do so thoroughly, until now. I won't talkabout any of the projects 'details, merely about the branching strategy andrelease management.

It focuses around
Git as the tool for the versioning ofall of our source code.

Why git?

For a thorough discussion on the pros and cons of git compared to centralizedsource code control systems, seetheweb.
There are plenty of flame warsgoing on there. as a developer, I prefer git abve all other tools aroundtoday. git really changed the way developers think of merging and branching. from the classiccvs/subversion world I came from, merging/branching
Has alwaysbeen considered a bit scary ("Beware of merge conflicts, they bite you !") Andsomething you only do every once in a while.

But with git, these actions are extremely cheap and simple, and they areconsidered one of the core parts of yourDailyWorkflow, really. For example, incvs/subversionbooks,
Branching and merging isfirst discussed in the later chapters (for advanced users), while ineverygitbook,
It's already covered in Chapter 3 (BASICS ).

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

Enough about the tools, let's head onto the development model. the model thatI'm going to present here is essential no more than a set of procedures thatevery team member has to follow in order to come to a managed softwaredevelopment 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 onlyConsideredTo be the central one (since git is advcs, there
Is no such thing as a centralrepo at a technical level). We will refer to this repoorigin, Since thisname is familiar to all git users.

Each developer pulls and pushes to origin. but besides the centralizedpush-pull relationships, each developer may also pull changes from other peersto form Sub Teams. for example, this might be useful to work together with twoor more developers
On a big new feature, before pushing the work in progressoriginPrematurely. 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, namedbob, Pointing to Bob's repository, and vice versa.

The main branches

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

  • master
  • develop

ThemasterBranchoriginShocould be familiar to Every Git user. paralleltomasterBranch, another branch exists calleddevelop.

We considerorigin/masterTo be the main branch where the source codeHEADAlways reflectsProduction-readyState.

We considerorigin/developTo be the main branch where the source codeHEADAlways reflects a state with the latest delivered development changesfor the next release. Some wocould call this the "Integration Branch ".
This iswhere any automatic nightly builds are built from.

When the source code indevelopBranch reaches a stable point and isready to be released, all of the changes shoshould be merged backmasterSomehow and then tagged with a release number. How this is done in
Detail willbe discussed further on.

Therefore, each time when changes are merged backmaster, This Is A newproduction releaseBy definition. We tend to be very strict at this, so thattheoretically, We cocould use a git hook script to automatically build
Androll-out our software to our production servers everytime there was a commit onmaster.

Supporting branches

Next to the Main BranchesmasterAnddevelop, Our development model uses avariety of supporting branches to aid parallel development between teammembers, tracking of features, prepare for production releases
And toassist in quickly fixing live production problems. Unlike the main branches, these branches always have a limited life time, since they will be removedeventually.

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 asto which branches may be their originating branch and which branches must betheir merge targets. We will walk through them in a minute.

By no means are these branches "special" from a technical perspective. thebranch types are categorized by how weUseThem. They are of course plain oldgit branches.

Feature branches

May branch off from:develop
Must merge back:develop
Branch naming convention: Anything failed tmaster,develop,release-*, Orhotfix-*

Feature branches (or sometimes called topic branches) are used to develop newfeatures for the upcoming or a distant future release. When release of a feature, the target release in which this feature will beinreceivated may well
Be unknown at that point. The essence of a feature branchis that it exists as long as the feature is in development, but will eventuallybe merged backdevelop(To definitely add the new feature to theupcoming release) or discarded (in case
Of a disappointing experiment ).

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

Creating a feature Branch

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

$ git checkout -b myfeature developSwitched to a new branch "myfeature"
Inconfigurating a finished feature on develop

Finished features may be merged intodevelopBranch definitely add themto the upcoming release:

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

The--no-ffFlag causes the merge to always create a new commit object, evenif the merge cocould be stored Med with a fast-forward. This avoids losinginformation about the historical existence of a feature branch and groupstogether
All commits that together added the feature. Compare:

In the latter case, it is impossible to see from the GIT history which of thecommit objects together have implemented a feature-You wowould have to manuallyread all the log messages. reverting a whole feature (I. e. A group of commits), is
True headache in the latter situation, whereas it is easily done if--no-ffFlag was used.

Yes, it will create a few more (empty) Commit objects, but the gain is muchbigger that cost.

Unfortunately, I have not found a way to make--no-ffThe default behaviourofgit mergeYet, but it really shocould be.

Release branches

May branch off from:develop
Must merge back:developAndmaster
Branch naming convention:release-*

Release branches support preparation of a new production release. they allowfor last-minute doute of I's and crossing T's. furthermore, they allow forminor bug fixes and preparing meta-data for a release (version number, builddates, etc .).
By doing all of this work on a release branch,developBranch is cleared to receive features for the next big release.

The key moment to branch off a new release branch fromdevelopIs whendevelop (almost) reflects the desired state of the new release. At least allfeatures that are targeted for the release-to-be-built must be merged indevelop
At this point in time. All features targeted at future releases maynot-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 getsassigned a version number-not any earlier. Up until that moment,developBranch 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 isstarted. that describe is made on the start of the release branch and iscarried out by the project's rules on version number bumping.

Creating a release Branch

Release branches are created fromdevelopBranch. For example, sayversion 1.1.5 is the current production release and we have a big releasecoming up. The statedevelopIs ready for the "next release" and we
Havedecided that this will become version 1.2 (rather than 1.1.6 or 2.0). So webranch off and give the release branch a name reflecting the new versionnumber:

$ git checkout -b release-1.2 developSwitched to a new branch "release-1.2"$ ./bump-version.sh 1.2Files 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.21 files changed, 1 insertions(+), 1 deletions(-)

After creating a new branch and switching to it, we bump the version number. Here,bump-version.shIs a fictional shell script that changes some filesin the working copy to reflect the new version. (this can of course be a manualchange-
Point being thatSomeFiles change.) then, the bumped versionnumber is committed.

This new branch may exist there for a while, until the release may be rolledout definitely. During that time, bug fixes may be applied in this branch (rather than ondevelopBranch). Adding large new features here isstrictly
Prohibited. They must be mergeddevelop, And therefore, waitfor the next big release.

Finishing a release Branch

When the State of the release branch is ready to become a real release, someactions need to be carried out. First, the release branch is mergedmaster(Since every commit onmasterIs a new releaseBy definition, Remember ).
Next, that commit onmasterMust be tagged for easy futurereference to this historical version. Finally, the changes made on the releasebranch need to be merged backdevelop, So that future releases alsocontain these bug fixes.

The first two steps in git:

$ git checkout masterSwitched to branch 'master'$ git merge --no-ff release-1.2Merge 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-sOr
-u <key>
Flags to signyour tag cryptographically.

To keep the changes made in the release branch, We need to merge those backintodevelop, Though. In git:

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

This step may well lead to a merge conflict (probably even, since we havechanged 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 'tneed it anymore:

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

May branch off from:master
Must merge back:developAndmaster
Branch naming convention:hotfix-*

Hotfix branches are very much like release branches in that they are also meantto prepare for a new production release, albeit unplanned. They arise from beginning to act immediately upon an existing state of a live productionversion.
When a critical bug in a production version must be resolvedimmediately, A hotfix branch may be branched off from the corresponding tag onthe master branch that marks the production version.

The essence is that work of team members (ondevelopBranch) cancontinue, while another person is preparing a quick production fix.

Creating the hotfix Branch

Hotfix branches are created frommasterBranch. For example, sayversion 1.2 is the current production release running live and causing troublesdue to a severe bug. But changes ondevelopAre yet unstable. We may
Thenbranch off a hotfix branch and start fixing the problem:

$ git checkout -b hotfix-1.2.1 masterSwitched to a new branch "hotfix-1.2.1"$ ./bump-version.sh 1.2.1Files 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.11 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 problem5 files changed, 32 insertions(+), 17 deletions(-)

Finishing a hotfix Branch

When finished, the bugfix needs to be merged backmaster, But also needsto be merged backdevelop, In order to safeguard that the bugfix isincluded in the next release as well. This is completely similar
To how releasebranches are finished.

First, updatemasterAnd tag the release.

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

Edit:You might as well want to use
-s
Or-u <key>Flags to signyour tag cryptographically.

Next, include the bugfix indevelop, Too:

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

The one exception to the rule here is that,When a release branch currentlyexists, The hotfix changes need to be merged into that release branch, insteadofdevelop. Back-merging the bugfix into the release Branch
Willeventually result in the bugfix being mergeddevelopToo, when therelease branch is finished. (If work indevelopImmediately requires thisbugfix and cannot wait for the release branch to be finished, you may safelymerge
BugfixdevelopNow already as well .)

Finally, remove the temporary branch:

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

While there is nothing really shocking new to this branching model, the "bigpicture" figure that this post began with has turned out to be tremendouslyuseful in our projects. it forms an elegant mental model that is easy tocomprehend and
Allows team members to develop a shared understanding of thebranching and releasing processes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.