Git Flow/Git Branching Model
Git Flow(or Gitflow) is a Git workflow that prescribes how a large-scale project can be managed by using different branching functionalities in Git. This workflow is mostly used for projects that have a fixed version release cycle. The Gitflow workflow uses five different types of branches to manage different aspects of a project. Let's learn more about this Git workflow.
Before trying to understand the actual workflow, let's first take a look at the different branches that will be used in Gitflow. We have five different types of branches that are used in this workflow - Master, Develop, Feature, Release, and Hotfix. All of them have dedicated functions and are created to free up other branches so that work can continue on them. Let's learn about each one of them.
This is the main branch of the project and it will have the production-ready code of the project. It will store all the version-release history of the project. All the commits of the master branch are tagged to denote the release version numbers. Remember that this branch will only contain a partial project history that involves the stable code. We will never commit anything directly to this branch.
The develop branch can be considered as another main branch where all the changes will be integrated by developers. It is also known as the Integration Branch. This branch contains the complete project history and acts as a buffer between the master branch and the feature branches. It contains the changes that will be part of the next release.
Developers will create a separate feature branch to work in an isolated environment and develop a particular feature for the project. These feature branches are created from the develop branches and merged back into it. They should never directly interact with the master branch of the project.
Once all the features needed for the next release are merged into the develop branch, a new release branch is created which will include all these features. This is done to free up the develop branch so that other collaborators can continue to develop new features and merge them into the develop branch. No major changes are added to the release branch and it will only include small bug fixes, documentation, and other meta-data required for the next release. Tests are performed on this release branch to make sure that the included code is stable. It is then merged into the master and develop branch.
Sometimes an immediate solution is required to a problem present in the master branch. For this, a new hotfix branch is created based on the corrupted commit of the master branch, and the problem is rectified on this branch. It is merged back into the master branch and a new version is released. This branch is also merged back into the develop branch to make sure that the develop branch is aware of the quick-fix that was created to solve the problem.
We now know the different branches involved in this Git workflow. The following steps explain how and when each of these branches will be created and used.
The project starts by initializing a Git repository with a master branch. We will add an initial tagged commit to it. The overall structure of the master branch is shown below.
Next, a develop branch is created based on the initial commit of the master branch. This branch is where all the development work for the project will be done. The master branch and the develop branch are called the Long-Running branches as they will always be present in the project. Other branches like feature, hotfix, and release are deleted after they serve their purpose.
Any new feature will have its own dedicated feature branch. This feature branch will be based on some commit of the develop branch. When the work on this branch is ready, it is merged back into the develop branch.
A new feature was developed and merged into the develop branch in the above image. We are now ready to create a new release. For this, a release branch is created based on the develop branch. We will add a few commits to this release branch which will include meta-data about the next release. This is then merged into the master and the develop branches.
Let's say we discover a small bug in some release version. To rectify it, we will create a new hotfix branch based on the erroneous commit of the master branch. After removing the bug this branch is merged back into master a new release is made. We also merge it into the develop branch.
A thing to remember is that all merges are Three-Way merges. This is done to ensure that we can understand where a new branch was created and where it was merged.
The final history of the repository is shown below. We have the master branch with three commits indicating that we have made three releases versions. The develop branch has all the new developments added to the project and a feature branch is also created and merged back into it. A bug was present in the initial version(v0.1) and it was rectified using a hotfix branch. We have a release branch made to add documentation and other release meta-data for version v1 of the project.
Advantages of using Gitflow Workflow
Gitflow workflow is very commonly used and there are certain benefits of using this workflow. Let's look at some of the key advantages of Gitflow.
- Gitflow allows developers to work parallelly on different things as we have dedicated branches for different tasks. For example, we have the hotfix branch and release branch which can be used to prepare a new release version. These branches will free up the develop branch and so new development can be continued on this branch. This also provides an isolated environment to work on different things.
- Feature branches provide developers an independent workspace where they can experiment with things and develop new features without worrying about affecting the rest of the project.
- Hotfix branches allow us to provide quick solutions to bugs present on the master branch. This separate branch also makes sure that we are only including the solution to the problem and not the newly developed features from the develop branch.
- The develop branch provides a kind of staging area for new features. All the new developments are added to this branch and they can then be included in the next release version.
Gitflow is a workflow that defines how to use different branches to work and collaborate on large-scale projects that need frequent release cycles. It simply defines a branching model and fixed roles for different branches. We have two main long-running branches called master and feature, and three temporary functionality branches called feature, release, and hotfix. These branches provide support parallel development which can increase the overall efficiency of the team.