Gitflow Workflow
The Gitflow Workflow (we’ll call it Gitflow from here forward) is much more involved than the first workflows we discussed. The workflow still uses the same set of commands that you’d need with the last workflow but it narrowly defines how branches are named and used.
Gitflow was created by Vincent Driessen and published in a widely read article on his website in January 2010.
In Gitflow, the goal is to create a solid software development workflow that is focused on predictable and stable software releases.
While Gitflow is more complicated than the other two workflows, it’s still standard Git; it comprises mostly of branching, adding, committing, and merging.
Let’s review the structure of Gitflow.
Branches Structure #
The main structure behind Gitflow is the branches setup. Gitflow has two different types of branches:
- Main Branches
- Supporting Branches
The Main Branches hold sacred versions of the project code. The Supporting Branches help ensure a smooth and predictable software development cycle. Let’s look at each a bit closer.
insert image of branching setup
Main Branches
There are two branches in the Main Branches category:
master
develop
We’re already familiar with the master
branch from earlier. In Gitflow, the master
branch has the following traits:
- always production-ready, release-ready
- every commit is a new release
- no work is done in
master
- limited options for branching off
master
The master
branch is the ultimate branch of the project and should always be treated as if you could ship production-ready code from it at any time. It represents the currently released state of your product. The master
branch has a tag for each release (see more under Supporting Branches).
Thedevelop
branch, on the other hand, isn’t quite so rigid. However, develop
should only contain changes that are considered “ready for next release.” It’s the branch from which you would cut nightly releases, or use as an integration branch.
develop
is also the source of the supporting branches. However, unlike in other workflows, develop
should not be used as branch in which you actively commit new changes. It’s name is confusing but it is not the branch in which you actually develop new code. That responsibility lies with the Supporting Branches.
Supporting Branches
There are three types of supporting branches:
- feature branches
- release branches
- hotfix branches
These branches serve as the active development branches where you add new features, make major changes, prepare releases, or quickly fix serious bug or issues post-release.
Let’s break down each branch type since they all have a set of guidelines on how to use them in a Gitflow project.
Feature Branches
Feature branches are where you do new work on features or other changes. These branches are typically local-only but some versions of Gitflow push all feature branches to the origin server so they can be merged server-side via a pull request.
Feature branches have the following rules:
- may branch from
develop
- must merge back in to
develop
- can be named anything you want. Common naming conventions are
feature/the-feature-name
orfeature_the-feature-name
Feature branches are where you will do your major coding work. You may still need to do some work inside of the release or hotfix branch but it should be minor changes.
Release Branches
The release branches are used to prepare a new software release. You may have to do some work directly in this branch, if you need to update project metadata (like bumping a version number) or other small tasks that are required to get a release out the door.
Release branches have the following rules:
- may branch from
develop
- must merge into
master
anddevelop
- should be named according to this convention:
release-*
where*
is the release version number. E.g.release-1.2.0
When a release is ready to go, the release branch will be merged into both master
and develop
. This is how you bring those branches up to speed with the current state of the project.
Hotfix Branches
Sometimes after you cut a release using a release branch and merge back into master
, tag the release, and distribute the software, you find a problem with the release. You need to quickly fix the problem and cut a new version. Rather than go through the standard workflow, you would use Hotfix branches.
Hotfix branches short circuit the standard workflow so you can quickly fix the problem and release an update.
Release branches have the following rules:
- may branch off master, via a tag for the release
- must merge back into master and develop
- should be treated like a release branch
- should be named according to this convention:
hotfix-*
where*
is the incremented release number
Once you have completed the work on the Hotfix branch you merge it back into develop
and then master
and tag the release in master
. You do not need to create a release branch since the hotfix branch acts as a release branch.
Step-by-Step #
Let’s step through the Gitflow workflow using our sample project and showing the commands that we’d use for a typical release cycle. This is simulated and truncated and just for demonstration purposes in this instruction. Your workflow will almost certainly be more complex than what I have written here.
- Our developer kicks off a new software development cycle by creating a new feature branch off of
develop
.git checkout -b feature/adding-the-widget develop
- With her new feature branch created and checkout out, she gets to work implementing the new widget feature. She commits her changes to the
feature/adding-the-widget
branch. - Meanwhile, her colleagues are also working on their own feature branches, coding away on their contributions to the next release.
- Our developer is now done with her feature and she alerts a co-worker to pull her changes and do a code review. The co-worker gives a green light and she merges the feature branch into
develop
.git checkout develop && git pull && git merge --no-ff feature/adding-the-widget
- Now that the new feature is in
develop
it will be ready for the next nightly build and included in the next release. - The release manager made a decision on the date and changes in the release and is ready to cut a new release from
develop
. A new release branch is created from thedevelop
branch.git checkout develop && git pull && git branch -b release-1.2.0
- The release branch is updated with any last minute changes like bumping the version numbers in files or metadata. When the release is ready, the
release-1.2.0
branch is merged back in to develop:git checkout develop && git pull && git merge --no-ff release-1.2.0
- Then the branch is merged into
master
:git checkout master && git pull && git merge --no-ff release-1.2.0
and tagged as a new release:git tag -a 1.2.0
- After the release is in the wild, the developers notice a critical issue that needs to be fixed immediately and included as part of a new release. They decide it’s worthy of a hotfix. They create a hotfix branch off of
master
.git checkout -b hotfix-1.2.1 master
They make the necessary changes and commit them:git commit -a -m "Hotfix 1.2.1 changes
- Once the critical hot fixes are done, the
hotfix-1.2.1
branch is merged intodevelop
:git checkout develop && git merge --no-ff hotfix-1.2.1
andmaster:
git checkout master && git merge --no-ff hotfix-1.2.1
- From
master
a new release is tagged:git tag -a v1.2.1
.
At this point the supporting branches that were created in the cycle can be deleted since they are all safely part of the both master
and develop
.