Codebeerstartups

Every Dog has one Blog

How to Use Git Flow in Your Projects

How to use git flow

Git is a version management tool and I am a big fan of it. I am hoping you came here for git flow. So you have basics of git in your hands. Again on the same note of good practicies, I think everyone should work on different branches and then keep merging those branches to main branch. But that becomes messy cause generally we forget to create branch. So here is a tool for you.

Note: I am talking about default branches here. You can change that naming convention but I will suggest not to.

So coming back to the topic of git flow.

Installation

Installing on Mac OS X

1
brew install git-flow

Installing on Ubuntu

1
apt-get install git-flow

Initiating Git flow:

1
git flow init

It will promt you something like this:

1
2
3
4
5
6
7
8
9
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? []

Keep pressing enter key to use the defaults. or just do

1
git flow init -d

for accepting all the defaults.

Whats the whole process:

  • First I want to be on a development branch and keep working on the various tasks of the projects.
  • For every task of the project we need to create a branch and then merge that branch into develop branch.
  • Once code is production ready. We need to create a release and test the code and make appropiate fixes.
  • Merge the fixed code into development branch and into production branch.
  • Deploy the code and start working on new tasks of the projects.
  • Meanwhile there may be a bug on the production server. We need to fix the code via hotfix and then merge the fixed code on development branch and production branch.
  • Now move back on the task we were actually doing before the hotfix. ;)

So now we have git flow ready in our git repository. Lets do things one by one.

Create a new feature branch:

For every task we are doing, we will be create a feature using git flow. Lets assume we want to implement login task.

1
git flow feature start login

It will create a new branch with feature namespace is feature/login

Now work on this branch and keep commiting your code. Now once done. You want to merge it to develop branch. Here is the command for the same.

1
git flow feature finish login

It will merge feature/login branch to develop branch and delete this feature/login branch.

Keep creating new feature branch. Keep working on it. Once done, finish that feature and it will be merge on develop branch. Push that develop branch, so that other people can pull it.

1
2
git push origin develop
git pull origin develop

Pushing / pulling a feature.

There may be cases when you need to push feature without merging it develop branch do that this by

1
git flow feature publish name_of_the_feature_like_we_had_login

if you want to pull any feature

1
git flow feature pull origin name_of_the_feature_like_we_had_login

Create a release and push the code to master branch ( by default master branch is treated as production branch)

Now we have code enough coding. We want to finally test the code and then need to push it on production branch. Lets create a release first so that we can test out the once and make appropirate changes if required.

1
git flow release start version_1

Now test the code and make any changes that you need to make. Commit those changes. Once done.

1
git flow release finish version_1

This will merge the code into develop branch + master branch and now we can deploy the code from master branch and then come back to develop branch to implement new tasks.

Hot fixes

Now we are working on new cool task and we are on a feature ie my_awesome_task and meanwhile we came to know that there is a bug in the production on the title of the page. Now we need to do a hotfix.

1
git flow hotfix start typo_bug_in_title

Now we will fix that title issue and then commit the code and then finish this hotfix.

1
git flow hotfix finish typo_bug_in_title

This will merge the code into master branch and into develop branch too. Now you can deploy the code using master branch and come back to your feature my_awesome_task and do the task. You can checkout to your feature branch that you want working on by

1
git flow feature checkout my_awesome_task

I hope this make some sense. Let me know if you have any doubts or anything.

Comments