Git flow
How to better organize your repos
Does that image look complicated? Click on the image below if you’d like to watch a quick video using Gitflow in GitKraken.
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
We consider origin/master
to be the main branch where the source code of HEAD
always reflects a production-ready state.
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.
Supporting Branches
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 master
, develop
, release-*
, 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.
Finished features may be merged into the develop branch to definitely add them to the upcoming release. 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:
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, 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 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.
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.
git checkout master # Switched to branch 'master'
git merge --no-ff release-1.2 # Merge made by recursive. (Summary of changes)
git tag -s 1.2
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 the develop branch) can continue, while another person is preparing a quick production fix.
Repeat steps above to merge into master
and develop
but bump the tag version by a minor.
git tag -s 1.2.1
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.)