GitFlow

written by Natalia on 2017-10-12

GitFlow is branching strategy created by Vincent Driessen. It helps with collaboration and parallel development. In this post I would like to focus on practical aspects.

Branches

There are two main branches master and develop. develop branch is where you can find all finished features that we are going to release with next release. master branch stores released version of project. It should be always synced with production.

There are also supporting branches: feature, hotfix and release.

You should create feature branch for every new feature. This branch is branched off of develop and after finishing feature it should be merged back to develop.

When you finish all features for next release, you should create release branch. This branch is also branched off of develop branch. After finishing release preparations, you should merge release branch to master and to develop branch as well.

There is also hotfix branch that are used to emergency fixes. Hotfix branches are created from masterbranch. After finishing it should be merged to master and develop.

As you can see the only commits on master branch are merges from release and hotfix branches. The only commits on develop branch are merges from feature, release and hotfix branches.

Tool

I am using commad line tool that helps with git flow. Here you can find installation instruction. It is a wrapper around git commands that will help you automate working with gitflow.

Starting repository

You can start your empty repository with git flow init command. This command initialises empty Git repository (git init) and creates branches master and develop. It will also ask you about prefixes for supporting branches. Finally it will checkout develop branch.

    16:27 > git flow init
Initialized empty Git repository in /Users/n/important-project/.git/
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
  

Starting new feature

As I mentioned before every feature has it own feature branch. To create new feature use command git flow feature start awesomeFeature. This command will create you branch feature/awesomeFeature out of develop and checkout this branch. Now you can write your code and commit with standard git commands.

Note:

Even if you are on feature/notSoAwesomeFeature branch (or any other branch) and you run this command it will create feature/awesomeFeature out of develop. Changes made on feature/notSoAwesomeFeature not merged to develop would not be present on feature/awesomeFeature branch.

    Switched to a new branch 'feature/awesomeFeature'

Summary of actions:
- A new branch 'feature/awesomeFeature' was created, based on 'develop'
- You are now on branch 'feature/awesomeFeature'

Now, start committing on your feature. When done, use:

     git flow feature finish awesomeFeature
  

Finishing feature

When you are done with your awesome feature just use git flow feature finish awesomeFeature as git flow told you. This command will merge your branch to develop branch. It will also checkout develop branch and remove your feature/awesomeFeature branch.

    16:43 > git flow feature finish awesomeFeature
Switched to branch 'develop'
Updating d83b277..0c428a2
Fast-forward
 awesomeFile | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 awesomeFile
Deleted branch feature/awesomeFeature (was 0c428a2).

Summary of actions:
- The feature branch 'feature/awesomeFeature' was merged into 'develop'
- Feature branch 'feature/awesomeFeature' has been removed
- You are now on branch 'develop'
  

Creating a release

Wow! All features are finished and tested? It is time to release your app. To create release branch use command git flow release start 1.0. This command creates new branch release/1.0 out of develop branch and checks out this branch. Now you can prepare your very important app to release: change code version, add needed fixes. But git flow will again tell you all of that:

Note:

Note: even if you are on feature/notSoAwesomeFeature branch and you run this command it will create release/1.0 out of develop. Changes made on feature/notSoAwesomeFeature not merged to develop would not be present on release/1.0 branch.

    20:28 > git flow release start 1.0
Switched to a new branch 'release/1.0'

Summary of actions:
- A new branch 'release/1.0' was created, based on 'develop'
- You are now on branch 'release/1.0'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:

     git flow release finish '1.0'
  

Finish release

After finishing your release preparations run command git flow release finish 1.0, as git flow told you . This command is doing some important things:

  • merges release/1.0 branch to master branch - which means all features you made and merged to develop branch are now merged to master branch.
  • merges release/1.0 branch to develop branch - which means all changes you made on release branch are merged to develop branch.
  • creates tag for this version of code
  • it also checks out master branch and removes release branch
    20:29 > git flow release finish 1.0
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 awesomeFile | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 awesomeFile
Deleted branch release/1.0 (was 0c428a2).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Release branch has been merged into 'master'
- The release was tagged '1.0'
- Release branch has been back-merged into 'develop'
- Release branch 'release/1.0' has been deleted
  

Fire on board - hotfix

Your app is released but something doesn't work good? Hotfix branch for the rescue. You can create hotfix branch with command git flow hotfix start terribleBugFix. This command will create branch hotfix/terribleBugFix branched off of master branch.

Now you can fix your bugs. Don't forget about changing your version.

Note:

Note: even if you are on feature/notSoAwesomeFeature branch and you run this command it will create hotfix/terribleBugFix out of master. Changes made on feature/notSoAwesomeFeature and changes made on develop would not be present on hotfix/terribleBugFix branch.

    20:50 > git flow hotfix start terribleBugFix
Switched to a new branch 'hotfix/terribleBugFix'

Summary of actions:
- A new branch 'hotfix/terribleBugFix' was created, based on 'master'
- You are now on branch 'hotfix/terribleBugFix'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

     git flow hotfix finish 'terribleBugFix'
  

Finishing hotfix

Finished with fixing bugs? Just run git flow finish hotfix terribleBugFix. This command:

  • merges hotfix/terribleBugFix branch to master branch - which means all fixes you made are now merged to master branch.
  • merges hotfix/terribleBugFix branch to develop branch - which means all fixes you made are also merged to develop branch.
  • creates tag for this version of code
  • it also checks out develop branch and removes hotfix branch
    21:01 > git flow hotfix finish terribleBugFix
Switched to branch 'develop'
Already up-to-date!
Merge made by the 'recursive' strategy.
Deleted branch hotfix/terribleBugFix (was e0fc4e4).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'master'
- The hotfix was tagged 'terribleBugFix'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/terribleBugFix' has been deleted
  

More?

If you initialised repository with git flow but decided to not to use it anymore just stop using git flow commands. As I mentioned before it is just a wrapper around git.

If you have an existing repository and want to use git flow just run git flow init. It will create all needed branches that are not present in your repository.

Those branch names feature/awesomeFeature are too long for you? When we first initialised our repository git flow asked about prefixes. Default is feature/ however you can use whatever fits you.

    21:40 > git flow feature start awesomeFeature
Switched to a new branch 'f/awesomeFeature'

Summary of actions:
- A new branch 'f/awesomeFeature' was created, based on 'develop'
- You are now on branch 'f/awesomeFeature'

Now, start committing on your feature. When done, use:

     git flow feature finish awesomeFeature
  

Finally

Git flow is really helpful branching strategy when you working in teams on project. I use it even on my personal project to keep things in order. I hope this post will help you with using git flow.

To find out more about gitflow visit author site: nvie.com