Git Flow

Learn via video courses
Topics Covered

Overview

Git Flow use feature branches and various primary branches to maintain the proper project development. Git flow was first published by Vincent Driessen at nvie. Git flow can be visualized as a set of rules or guidelines that the developers follow during project development in Git. A git flow should ideally be used in the projects that are supposed to have scheduled release cycles and continuous delivery per the best practices of DevOps. We should use this workflow technique when our work has multiple streams and our work is concurrently running.

Pre-requisites

The prerequisites for learning the git flow can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git flow command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems: Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

What Is Git Flow?

Whenever we are working as a team we must define some set of rules regarding the development in particular pushing updates to the master branch. The rules not only make the development phase easier but also makes the work of the maintainer quite easy. So, we must agree on some set of rules for example when should we create branches, naming convention, development team, bug fixing team, pulling and pushing strategy, etc. Now, this set of rules or workflow in terms of it is known as git flow. Git flow is a type of workflow that is not a necessary thing to be adapted but it can make development easier so we should use it.

Git flow can be visualized as a set of rules or guidelines that the developers follow during project development in Git. Some developers also term the Git flow as a branching model since the developers create branches, perform their work, and then push those branches to the central branch. Refer to the image provided below for more clarity.

What is git flow

An alternative to the Git branching model is Git flow. Git flow uses feature branches and various primary branches to maintain the proper project development. Git flow was first published by Vincent Driessen at nvie. The git flow consists of numerous long-lived branches having a large set of commits. As a branch contains numerous commits so a developer can create a branch (for example feature branch orbug-fixingg branch) and make commits in that branch only. After the feature development is completed then the developer can push the feature branch to the main branch or central branch. In terms of Git flow, the central branch is also known as the trunk branch because the flow can be visualized as a tree and in a tree trunk is the central component.

Now as we can see that we only push the development branches when the entire development is complete so a feature branch can contain a very large set of commits. These large sets of commits of the feature branch require more collaboration before merging. The merging can be tedious work and thus there is a high risk of deviation from the central trunk branch. Hence there are high chances of merge conflicts.

What is a merge conflict? Well, a merge conflict is an issue that arises when we try to merge two branches that have been edited at the same time and in the same file (refer to the image provided below for more clarity), then Git will not be able to merge them as Git cannot identify the version that needs to be used for changes.

In case of a merge conflict, Git stops the merging process just before performing the merge conflict so that the user can resolve the conflict manually. Refer to the image provided below for more clarity.

What is git flow 2

Now a question comes to our mind- when should we use the git flow? A git flow should ideally be used in the projects that are supposed to have scheduled release cycles and have a continuous delivery as per the best practices of DevOps. We should use this workflow technique when our work has multiple streams and our work is concurrently running.

How Does It Works?

As we have discussed earlier that git flow is a set of commands and rules that helps us to keep the track of feature branches, project releases, and hotfixes. In git flow we have two branches for example main and the develop branch that helps us to store all the records of the project's history. As the name suggests the main or master branch is the branch that contains all the official releases of the project.

On the other hand, there is an extra branch (for example a development branch named develop), this branch will contain all the integrations and development of the features. For easier development and future merging, we can use tags and version numbers for the main branch.

Refer to the image provided below for more clarity.

how git flow works

Now for the development, we should first create an empty development branch in our local repository and push that empty development branch to the central repository. The overall commands for the same are:

As we have discussed, we will use this develop branch to store the entire development history and apart from this, we will use the main branch or the trunk branch to store the abridged versions. Other developers should clone the central remote repository and create a tracking branch for the develop branch.

Installing Git Flow

Let us now learn how we can install a git flow. But before that, we must know that git flow brings us a set of extra commands which helps us to perform several tasks automatically and in a predefined order. In this article let's use one of the most popular git-flow i.e AVH edition.

Now to install git-flow we can use the following command:

  1. In the OSX:
  1. In Linux:
  1. In Windows:

We can to the git-flow AVH Edition official documentation for more details.

Setting Up Git Flow In A Project

Let us now learn how we can initialize a git flow and set up our project for the same.

The git flow initialization does not change our repository but it adds various commands so that our development can become easier. So, to add a git flow, we need to navigate to our git repository and use init to initialize the git flow in our repository. The overall command for the same is:

The git flow init is an interactive command. Git will ask some questions like:

These questions are asked for the configuration and some naming conventions of our branch. All the developers of a project should use a common naming convention for better development and easy work.

For example, let us take a demo and try to run the git init command. Once we run the command, git will ask for the names of branches, hooks, and filters. We can provide our desired names else we can press Enter to let git use the default names. Refer to the demo example image provided below for more clarity.

Setting up git flow in a project

Branching Model

Let us now learn a bit about the branching model that we have discussed in the previous sections.

In the git flow, we have two branches namely the main branch which is the central branch (or the trunk branch) apart from that we have the develop branch. let us briefly discuss these branches.

1. Main Branch

The main branch stored the production code and we must not hamper the production code. We should maintain a version of this branch and should only integrate the changes from the development branch when the feature is completely created. As the feature is completed, we can push the changes in the production code (main branch) so that the users can use the latest features, and hence users won't face any issues during the development phase. This will help us to maintain a proper code and we must use some tags to name the latest version so that if any issue comes shortly for a certain version then we can get back to the previously developed stable version and the bug can be fixed without hampering the stable code base.

2. Develop Branch

Another development branch should also be created and whenever we start working on a new feature then we must create a feature branch from it. After integrating the changes, we can push it to the development branch. And after testing the latest feature or the bug fixing on the develop branch, we can ultimately push the changes to the main production branch.

Refer to the image provided below for more clarity. develop branch

In the development phase, we have to create several feature branches. So, we can create a feature branch in two ways:

  1. Using normal git command: We will first change the current branch to the develop branch and then we can create the feature branch. To overall command for the same is:
  2. Using the git flow command: We can combine both the above commands and use the git-flow extension to create a feature branch. To overall command for the same is:

Note: The develop and the main branch should remain in our project as they are also called long-running branches. But the other branches like features, releases, etc exist temporarily. Hence, they will be deleted once their purpose is fulfilled.

GitFlow Commands

Let us now learn the commands that are associated with the git flow.

1. init

The git flow init command is used to initialize the git flow on a git repository. We have several flags associated with the git flow init command. Some of the important flags or options associated with this command are:

  • -h or --help: This flag will show the help related to the current branch.
  • --showcommands: This flag will show the commands while the commands are executed.
  • -d or --defaults: This flag will make git use the default naming convention.
  • -f or --force: This flag will forcefully set the git flow branches even if the branches are already configured.

2. Feature

The git flow feature command is used to list down all the feature branches of the current workflow. Some of the important flags or options associated with this command are:

  • -h or --help: This flag will show the help related to the current branch.
  • -v or --verbose: This flag will show more output or verbose.

Create A New Feature Branch

We can start a new feature branch using the following command.

We can use flags like: -h, --showcommands, -F, etc.

Track A Feature Branch

We can track a feature branch using the following command.

We can use flags like: -h, --showcommands, etc.

Finish A Feature Branch

We can finish a feature branch using the following command.

We can use flags like: -h, -F, -r, -p, -k, -D, -S, etc.

Please refer to the official documentation for more details about the feature command.

3. Release

The git flow release command is used to list down all the release branches of the current workflow. Some of the important flags or options associated with this command are:

  • -h or --help: This flag will show the help related to the current branch.
  • -v or --verbose: This flag will show more output or verbose.

Create A New Release Branch

We can start a new release branch using the following command.

We can use flags like: -h, --showcommands, -F, etc.

Track A Release Branch

We can track a release branch using the following command.

We can use flags like: -h, --showcommands, etc.

Finish A Release Branch

We can finish a release branch using the following command.

We can use flags like : -h, -F, -s, -u, -m, -f, -p, -n, -k, -b, -S, etc.

Please refer to the official documentation for more details about the release command.

4. Hotfix

The git flow hotfix command is used to list down all the hotfix branches of the current workflow. Some of the important flags or options associated with this command are:

  • -h or --help: This flag will show the help related to the current branch.
  • -v or --verbose: This flag will show more output or verbose.

Create A New Hotfix Branch

We can start a new hotfix branch using the following command.

We can use flags like: -h, --showcommands, -F, --fetch, etc.

Finish A Hotfix Branch

We can finish a hotfix branch using the following command.

We can use flags like : -h, -F, -s, -u, -m, -f, -p, -n, -k, -b, -S, etc.

Please refer to the official documentation for more details about the hotfix command.

5. Support

The git flow support command is used to list down all the support branches of the current workflow. Some of the important flags or options associated with this command are:

  • -h or --help: This flag will show the help related to the current branch.
  • -v or --verbose: This flag will show more output or verbose.

Create A New Support Branch

We can start a new support branch using the following command.

We can use flags like: -h, --showcommands, -F, --fetch, etc.

Please refer to the official documentation for more details about the support command.

Advantages of Git Flow

So far we have discussed a lot about git flow and its usage, let us now learn about the advantages of git flow.

  • Git flow is quite convenient to use as it provides us with a lot of commands that speed up our development work.
  • We can easily keep track of the entire development phase as well as the previous releases of the project.
  • We can easily switch among the branches.
  • Git flow promotes a cleaner code base and proper structuring of the central repository.
  • Git flow commands can perform multiple works but if we do not use the git-flow then we have to write separate commands for all the tasks.
  • Due to the creation of multiple branches, the trunk branch or the central branch does not get affected.
  • Git flow manages the overall structuring of branches, release of the projects, features additions, and bug fixing without hampering the production code.

Using Git Flow Commands Vs Raw Git Commands

Let us now see the commands that are used in git flow and the corresponding raw commands:

Initialize

git flowgit
git flow initgit init
---git commit --allow-empty -m "Initial commit"
---git checkout -b develop master

To Connect With the Remote Repository

git flowgit
---git remote add origin git@github.com:MYACCOUNT/MYREPO

Features

To Create Feature Branches

git flowgit
git flow feature start MYFEATUREgit checkout -b feature/MYFEATURE develop

To Share Feature Branches

git flowgit
git flow feature publish MYFEATUREgit checkout feature/MYFEATURE
---git push origin feature/MYFEATURE

To Get the Latest Update For Feature Branches

git flowgit
git flow feature pull origin MYFEATUREgit checkout feature/MYFEATURE
---git pull --rebase origin feature/MYFEATURE

To Finalize Feature Branches

git flowgit
git flow feature finish MYFEATUREgit checkout develop
---git merge --no-ff feature/MYFEATURE
---git branch -d feature/MYFEATURE

To Push the Merged Feature Branches into Origin

git flowgit
---git push origin develop
---git push origin: feature/MYFEATURE (if pushed)

Releases

To Create Release Branches

git flowgit
git flow release start 1.2.0git checkout -b release/1.2.0 develop

To Share The Release Branches

git flowgit
git flow release publish 1.2.0git checkout release/1.2.0
---git push origin release/1.2.0

To Get The Latest Update For Release Branches

git flowgit
---git checkout release/1.2.0
---git pull --rebase origin release/1.2.0

To Finalize the Release Branches

git flowgit
git flow release finish 1.2.0git checkout master
---git merge --no-ff release/1.2.0
---git tag -a 1.2.0
---git checkout develop
---git merge --no-ff release/1.2.0
---git branch -d release/1.2.0

To Push The Merged Release Branches into Origin

git flowgit
---git push origin master
---git push origin develop
---git push origin --tags
---git push origin: release/1.2.0

Hotfixes

To Create Hotfix Branches

git flowgit
git flow hotfix start 1.2.1 [commit]git checkout -b hotfix/1.2.1 [commit]

To Finalize the Hotfix Branches

git flowgit
git flow hotfix finish 1.2.1git checkout master
---git merge --no-ff hotfix/1.2.1
---git tag -a 1.2.1
---git checkout develop
---git merge --no-ff hotfix/1.2.1
---git branch -d hotfix/1.2.1

To Push the Merged Hotfix Branches Into Origin

git flowgit
---git push origin master
---git push origin develop
---git push origin --tags
---git push origin:hotfix/1.2.1

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • Git flow uses feature branches and various primary branches to maintain the proper project development. Git flow was first published by Vincent Driessen at nvie.
  • We should use this workflow technique when our work has multiple streams and our work is concurrently running.
  • A git flow should ideally be used in the projects that are supposed to have scheduled release cycles and have a continuous delivery as per the best practices of DevOps.
  • Git flow can be visualized as a set of rules or guidelines that the developers follow during project development in Git.
  • In git flow we have two branches for example main and the develop branch that helps us to store all the records of the project's history.
  • The main or master branch is the branch that contains all the official releases of the project. For easier development and future merging, we can use tags and version numbers for the main branch.
  • The develop branch contains all the integrations and development of the features.
  • The git flow initialization does not change our repository but it adds some extra commands so that our development can become easier.
  • The git flow init command is used to initialize the git flow on a git repository.
  • The git flow feature command is used to list down all the feature branches of the current workflow. Similarly, we have the commands to list down release branches, and hotfix branches.