Git Flow

What is git flow?

If you are familar with git then you might know about common issues with git.

If you dont follow the best git practises you may end up having merging issues and in some case loosing other developers code. Other developers may freak out on you but wont tell you in front.

Git flow is a standard process which avoids this confussion. Once you learn how to use git properly using git flow process you will no longer have issues with merging and loosing code.

How to install Git flow?

# install git flow on mac
$ brew install git-flow-avh

# install git flow on linux
$ apt-get install git-flow​

Getting started with git flow

Let's learn about git flow in detail let's run following commands to get started with git flow:

# let's create a new git project
$ mkdir my-project

# let's initialize git
$ cd my-project
$ git init

# initialize git flow
$ git flow init​

During git flow initilialization process you will be asked some question as shown in below image:

Keep the default settings and finish the installation. Git flow will now have following file structure to follow:

|
|__ feature/     # all the new features will go in this branch
|__ release/     # new prod releases will be done here
|__ hotfix/      # hotfixes will go here
|__ support/    
|__ bugfix/
|__ master       # your master branch
|__ develop      # your develop branch​

How git flow works?

To understand how git flow works let's understand following steps:

  • You are assigned a task by your company
  • all developer works on develop branch
  • You create a feature branch for your task from develop branch
  • You are done with your changes and now ready to merge your feature in develop branch
  • You merged your feature branch with develop branch
  • deployment manager will merge develop branch to master branch
  • your master branch is used by production servers

What is the benefit of using git flow process?

Think of these way you work for a bigger company where all developers work on just one branch what would happen?

If they do not take proper precautions they often get conflicts and other devs will shout at each other because their code is gone during conflicts.

What if now, they all work on different branches and later they merge their changes to master branch? There are less chances of getting conflicts now.

Git flow does the same thing it follows certain pattern to avoid common issues with git.

How to create a feature branch using git flow?

You can create a new feature branch using start command and end the feature using end command.

# create a new feature from develop
$ git flow feature start login-feature

# you make some changes to your feature
# branch and ready to merge your feature
$ touch login.html
$ git add login.html
$ git commit -m "login page added"

# push your feature branch to remote
$ git flow feature publish finish login-feature

# merge your feature brach back to develop branch
$ git flow feature finish login-functionality​

How to deploy using git flow?

Once you merged your feature branch to your develop branch your next task is to deploy your code to production.

You have to create a new release and keep track of all deployed branches just for backup.

# let's create a new release 
# i.e getting your changes from develop to master
$ git flow release start 2017_01_02

# publish your release branch
$ git flow release publish 2017_01_02

# finish up a release
# it merges the release branch back into master
# tags the release with its name
# removes the release branch
$ git flow release finish 2017_01_02

# don't forget to push your tags
$ git push --tags​

How to create a hotfix using git flow?

Imagine a scenario where you deployed some changes to production server. Something went wrong with deployment and there is a major bug that needs to be fixed immediatly.

You might not want to follow the process below:

  • Creating a new feature branch
  • Merging feature into develop branch
  • Creating a new release branch
  • Merging develop to master

Rather you want to follow the steps below to quickly fix the bug:

  • Create a hotfix branch
  • Merge your hotfix branch directly to master/develop

That is what a hotfix does. It skips the process of merging a feature to develop rather a hotfix branch is merged to master and develop branch at the same time.

# start a hotfix
$ git flow hotfix start issue-with-login

# now you are on hotfix branch do your changes
# and commit your changes
$ git add login.html
$ git commit -m "login bug fixed"

# finish your hotfix
# merge hotfix to master/develop at same time
$ git flow hotfix finish issue-with-login​

Reference Link

git-flow cheatsheet