DEV Community

Cover image for A practical introduction to Git Flow
Luca
Luca

Posted on • Updated on

A practical introduction to Git Flow

Most part of the devs out there use Git everyday but for who is not familiar with it, Git is the most popular version system control.

Git basically works by tracking the changes made to the files providing a record of what has been done and allowing to revert to a specific version any time you want.
Git also makes collaboration easier. It enables all changes to be merged into one single source.
These possibilities combined with its ease of use have made it a needful tool to handle the life cycle of the development of a project.

There are various strategies for working with Git and and you can choose among them depending on your own context and needs.
Personally, I have used many times the Git Flow branching model which is described in this great article by Vincent Driessen.

Git Flow
The image above, taken from the article by Driessen, shows an example of the flow of this branching model.

This strategy implies the use of the following types of branches:

  • master: it is the branch that reflects the production state. Starting from 2020 it is often named main.
  • develop: is the principal branch where the state of code represents the latest delivered development changes for the next release.
  • feature/<feature-name>: these branches are completely dedicated to the development of new functionalities. Feature branches come from the develop branch and must be merged back to the develop branch.
  • release/<release-name>: the release branches are used to prepare the release of a new version. These branches start from the develop branch and must be merged in both develop and master branches.
  • hotfix/<hotfix-name>: the hotfix branches are used to make urgent corrections to the project. These branches come from the master branch and must be merged back in the master and the develop branches.

 

git-flow is a set of Git shortcuts to easily apply this branching model, you don’t have to use it necessarily but it is very convenient.

For information about the installation refer to the official repo.

To work with git-flow you need first to initialize it in the project by using



git flow init


Enter fullscreen mode Exit fullscreen mode

Now let's see how to do the most common activities
 

Add a new feature

You need to create a new feature branch from the develop branch and switch to it. You can achieve this with



git flow feature start <feature-name>


Enter fullscreen mode Exit fullscreen mode

After you have done all the needed changes to the feature, you can close it by typing



git flow feature finish <feature-name>


Enter fullscreen mode Exit fullscreen mode

The feature branch will be merged in the develop branch and then be deleted.
The pointer will be changed to the develop branch.

Since these actions are performed in your local repository, always remember to push the changes when you finish your features to update the remote develop branch.
Alternatively to push you can use



git flow feature publish <feature-name>


Enter fullscreen mode Exit fullscreen mode

 

Make a new release

First you need to start a new release with the command



git flow release start <release-name>


Enter fullscreen mode Exit fullscreen mode

that creates a new release branch from the develop branch and switches to it.

Once you have done all the changes needed you can close the release with



git flow release finish <release-name>


Enter fullscreen mode Exit fullscreen mode

the release will be merged in the master branch and a version tag will be created using the name of the release branch.
Merges the release in develop and delete the release branch.

Here, too, remember to push the branches and the new tag to update the remote branches. More simply, you can also use



git flow release publish <release-name>


Enter fullscreen mode Exit fullscreen mode

 

Apply a hotfix

Like in the other examples we need first to create a new hotfix branch from master branch and then switch to it with the command



git flow hotfix start <hotfix-name>


Enter fullscreen mode Exit fullscreen mode

When the hotfix is ready to be closed we can use



git flow hotfix finish [<tag version-name>]


Enter fullscreen mode Exit fullscreen mode

Under the hood this command will merge the hotfix into the master and develop branches, delete the hotfix branch and create a new tag.

Here, too, it is important to update the remote branches.



(master) $ git push
(master) $ git push --tags
(master) $ git checkout develop
(develop) $ git push
$ git push {github_username} :hotfix/1.3.4


Enter fullscreen mode Exit fullscreen mode

 

If you are looking for a brief summary of git-flow commands check this cheatsheet

👋 See you in the next article!

Learn more

Top comments (0)