Skip to content
Gary Rowe edited this page Aug 9, 2013 · 1 revision

Branching strategy

See the excellent description of the "master-develop" strategy for a thorough treatment, including worked examples. Alternatively, use the short version below which is a little simplified, and targets a Maven build process.

GitHub branching strategy

The main branches

In "master-develop" branches are named and used as listed below. GitHub acts as origin since it is the repository used as the basis for CI builds and deployment.

  • origin/master - this is the main branch where the source code of HEAD always reflects a production ready state. It has an infinite lifetime.
  • origin/develop - this is parallel to the main branch where the source code of HEAD always reflects the next production candidate. It has an infinite lifetime.

From a Maven perspective, the develop branch always has a version of develop-SNAPSHOT.

Supporting branches

Next to the main branches are the supporting branches. These are categorised as:

  • Feature branches - May branch off develop and must merge back into it. Has a finite lifetime. Are typically only present in developer's repositories. Support creation of new features.
  • Release branches - May branch off develop and must merge back into develop or master. Are typically present in origin and developer's repositories. Support the final touches needed to take develop into production without affecting ongoing work on develop.
  • Hotfix branches - Must branch off master and must merge back into develop and/or master.

There is nothing inherently special about these branches, they simply reflect how they are used by the development team to achieve their objectives of getting to a successful release.

Feature branches (myfeature)

  • May branch off from: develop
  • Must merge back into: develop
  • Branch naming conventions: anything except master, develop, release-* or hotfix-*

Feature branches are used to develop new features for an 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. 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. This gives you a starting point from the next production candidate.

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

You can now start working against this branch, filing commits against issues.

Incorporating a finished feature on develop

Finished features may be merged into the develop branch when you want to add them to the next production candidate. The steps to do this are

$ 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

Important 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. This means that rolling back develop to remove the merge from the feature branch is much less work (using the default behaviour means that a manual read of the commit comments is required).

Note that once the feature branch has been merged into develop the local repository has no need of it, and it can be safely deleted. There is no point cluttering up your local repository with dead branches - burn them!

Maven notes

In feature branches the Maven version usually remains at develop-SNAPSHOT since there is no way to know what the destination release is going to be. However, it may be necessary to share the feature development with other members of the team working on a downstream issue that depends on it. In this case, a version of <issue-number>-develop-SNAPSHOT (e.g. "EX-1-develop-SNAPSHOT") is used. This makes it very clear to developers that they are working against a fragile dependency. Once this issue is merged with develop the downstream issue dependency can be changed back to develop-SNAPSHOT and the "feature artifact" deleted from the Maven team repository.

Release branches (release-*)

  • 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 the last-minute dotting of i’s and crossing of t’s. Furthermore, they allow for minor bug fixes and preparing meta-data for a release (switching version number out of -SNAPSHOT, entering build dates, etc.). By doing all of this work on a release-* branch, the develop 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.0 or 1.0.0, until the release-* branch is started and therefore named (e.g. release-0.3.0 or release-1.0.0).

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 there is 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.0 (rather than 1.1.6 or 2.0.0). So we branch off and give the release branch a name reflecting the new version number:

$ git checkout -b release-1.2.0 develop
Switched to a new branch "release-1.2.0"
$ ./bump-version.sh 1.2.0
Files modified successfully, version bumped to 1.2.0.
$ git commit -a -m "Bumped version number to 1.2.0"
[release-1.2 74d9424] Bumped version number to 1.2.0
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. Typically, this would be part of a Maven-controlled release process but that may be managed in turn by a script.

This new branch may exist "in limbo" for a while, until the decision to roll it out is made. During that time, bug fixes may be applied in this branch (rather than on the develop branch which could by now be completely incompatible). 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 final 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). Next, that commit on master must be tagged for easy future reference to this historical version. Finally, the incremental changes made on the release-* branch need to be merged back into develop, so that future releases also contain these bug fixes or enhancements.

The first two steps in Git:

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

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

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.0
Merge made by recursive.
(Summary of changes)

This step is very likely to lead to a merge conflict since we have changed the version number. Make the necessary fixes and commit.

This completes all the release work, so the release branch may be removed since we don’t need it anymore (it's all in master):

$ git branch -d release-1.2.0
Deleted branch release-1.2.0 (was ff452fe).
Maven notes

In release branches the Maven version is changed to reflect the target release name, but still at a snapshot (e.g. "1.2.0-SNAPSHOT") is used. This makes it very clear to the testing team that they are working against a release candidate. Once the testing is complete and the release is good to merge into master then the last commit is to change the version out of snapshot (e.g. "1.2.0").

Hotfix branches (hotfix-*)

  • 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 a problem with the 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 the develop branch) can continue, while another person is preparing a quick production fix.

Creating a hotfix branch

Hotfix branches are created from the master branch. For example, say version 1.2.0 is the current production release running live and causing troubles due to a severe bug. But changes on develop are unstable and will take a while to resolve. 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(-)

Important: 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 fix needs to be merged back into master, but also needs to be merged back into develop, in order to safeguard that the fix is included in the next release as well. This is basically the same as 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

Next, include the fix 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)

Important: 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 fix into the release branch will eventually result in the bugfix being merged into develop too, when the release branch is finished. Of course, if work in develop immediately requires this fix and cannot wait for the release branch to be finished, you may safely merge the fix 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).
Maven notes

In hotfix branches there should be no breaking API changes. Consequently, the Maven version can be raised one revision level and moved into snapshot to allow multiple quick commits to occur (e.g. "1.2.1-SNAPSHOT"). Once the testing is complete and the release is good to merge back into master then the last commit is to change the version out of snapshot (e.g. "1.2.1").