Getting started with git-flow to automate git branching workflow

Reading Time: 6 minutes

This blog shows the basic usage and effect of git-flow operations.

git-flow

git-flow are a set of git extensions to provide high-level repository operations for Vincent Driessen’s branching model. It has attracted a lot of attention because it is very well suited to collaboration and scaling the development team.

Key Features:

  • Git flow provides excellent command line help and output
  • Git-flow is a merge based solution. It doesn’t rebase feature branches
  • One of the great things about GitFlow is that it makes parallel development very easy, by isolating new development from finished work
  • Collaboration make it easier for two or more developers
  • Better support for release staging area
  • Support for emergency fixes at any time of development

Prerequisites:

  • Need a working git installation. To install it check Installing Git
  • Need a working git repository. We can use it in existing projects, but let’s start a new repository: playing-git-flow

Installing git-flow:

Choose your platform:

  • Mac OS X
  • Linux (and Unix, etc.): We are using Ubuntu machine. So that install with below command
$ apt-get install git-flow

Initialize git-flow:

Start using git-flow by initializing it inside an existing git repository:

You’ll have to answer a few questions regarding the naming conventions for your branches.
It’s recommended to use the default values.

$ git flow init

Which branch should be used for bringing forth production releases?
   - master
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? [] 
Hooks and filters directory? [/knoldus/playing-git-flow/.git/hooks] 

git-flow is just a wrapper around existing git commands, so the init command doesn’t change anything in your repository other than creating branches for you. If you don’t want to use git-flow anymore, there’s nothing to change or remove, you just stop using the git-flow commands.

If you run git branch after setting up, you’ll notice that you switched from the master branch to a new one named develop.

$ git branch
* develop
  master

The develop branch is intended to be the default branch where most of the work will happen, and the master branch keeps track of production-ready code. So instead of working in the master branch, you’ll use git push origin develop to push new code to your repository from now on.

Feature branches: Let’s start working with git-flow feature

  • Start a new feature branch: git-flow makes it easy to work on multiple features at the same time by using feature branches. To start one, use feature start with the name of your new feature like below example: I have a github issue to update readme with git-flow information. So I am going to start new feature branch with that particular issue.
$ git flow feature start issue#1
Switched to a new branch 'feature/issue#1'

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

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

     git flow feature finish issue#1

As the output already explains, you’re now on a new branch you can use to work on your feature. Use git like you normally would, like add your development code and commit it on the same feature branch.

$ git status
On branch feature/issue#1
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git add .
$ git commit -m "#1 - Updated README"
[feature/issue#1 02585b5] #1 - Updated README
 1 file changed, 77 insertions(+)
  • Publish an existing feature: If you are developing a feature in collaboration then
    Publish a feature to the remote server so it can be used by other users.
$ git flow feature publish issue#1
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.73 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:knoldus/playing-git-flow.git
 * [new branch]      feature/issue#1 -> feature/issue#1
Already on 'feature/issue#1'
Your branch is up-to-date with 'origin/feature/issue#1'.

Summary of actions:
- A new remote branch 'feature/issue#1' was created
- The local branch 'feature/issue#1' was configured to track the remote branch
- You are now on branch 'feature/issue#1'
  • Getting a published feature: Get a feature published by another user.
$ git pull
$ git checkout feature/issue#1 
Switched to branch 'feature/issue#1'
Your branch is up-to-date with 'origin/feature/issue#1'.
  • Finish up a working feature: Finish the development of a feature. This action performs the following:
    • Merged issue#1 into ‘develop’
    • Removes the feature branch
    • Switches back to ‘develop’ branch
$ git flow feature finish issue#1
Switched to branch 'develop'
Updating 5eb3520..02585b5
Fast-forward
 README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
To git@github.com:knoldus/playing-git-flow.git
 - [deleted]         feature/issue#1
Deleted branch feature/issue#1 (was 02585b5).

Summary of actions:
- The feature branch 'feature/issue#1' was merged into 'develop'
- Feature branch 'feature/issue#1' has been locally deleted; it has been remotely deleted from 'origin'
- You are now on branch 'develop'

Versioned releases: Let’s start a new production release

To start a release, use the git flow release command. It creates a release branch created from the ‘develop’ branch.

  • Start new release
$ git flow release start 1.0.0
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Switched to a new branch 'release/1.0.0'

Summary of actions:
- A new branch 'release/1.0.0' was created, based on 'develop'
- You are now on branch 'release/1.0.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.0'
  • Publish existing release

It’s wise to publish the release branch after creating it to allow release commits by other developers. Do it similar to feature publishing with the command:

$ git flow release publish 1.0.0 
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:knoldus/playing-git-flow.git
 * [new branch]      release/1.0.0 -> release/1.0.0
Switched to branch 'release/1.0.0'
Your branch is up-to-date with 'origin/release/1.0.0'.

Summary of actions:
- A new remote branch 'release/1.0.0' was created
- The local branch 'release/1.0.0' was configured to track the remote branch
- You are now on branch 'release/1.0.0'
  • Finish release

Finishing a release is one of the big steps in git branching. It performs several actions:

  • Merges the release branch back into ‘master’
  • Tags the release with its name
  • Back-merges the release into ‘develop’
  • Removes the release branch
$ git flow release finish 1.0.0 
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Switched to branch 'develop'
Already up-to-date.
To git@github.com:knoldus/playing-git-flow.git
 - [deleted]         release/1.0.0
Deleted branch release/1.0.0 (was 02585b5).

Summary of actions:
- Release branch 'release/1.0.0' has been merged into 'master'
- The release was tagged '1.0.0'
- Release tag '1.0.0' has been back-merged into 'develop'
- Release branch 'release/1.0.0' has been locally deleted; it has been remotely deleted from 'origin'
- You are now on branch 'develop'

Note: Don’t forget to push your tags with

$ git push --tags

Do the hotfixes

  • Hotfixes arise from the necessity to act immediately upon an undesired state of a live production version
  • May be branched off from the corresponding tag on the master branch that marks the production version.

Hotfix branches are a lot like release branches, except they’re based on master instead of develop. You’re automatically switched to the new hotfix branch so you can start fixing the issue and bumping the minor version number. When you’re done, hotfix finish:

  • To start hotfixing

Like the other git flow commands, a hotfix is started with

$ git flow hotfix start 1.0.1
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Switched to a new branch 'hotfix/1.0.1'

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

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

     git flow hotfix finish '1.0.1'
  • To finish hotfixing

By finishing a hotfix it gets merged back into develop and master. Additionally the master merge is tagged with the hotfix version.

$ git flow hotfix finish 1.0.1
Branches 'master' and 'origin/master' have diverged.
And local branch 'master' is ahead of 'origin/master'.
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Switched to branch 'develop'
Already up-to-date!
Merge made by the 'recursive' strategy.
Deleted branch hotfix/1.0.1 (was 6ebe5cf).

Summary of actions:
- Hotfix branch 'hotfix/1.0.1' has been merged into 'master'
- The hotfix was tagged '1.0.1'
- Hotfix tag '1.0.1' has been back-merged into 'develop'
- Hotfix branch 'hotfix/1.0.1' has been locally deleted
- You are now on branch 'develop'

Repository network graph

repo_network_graph

References

Now we know about git-flow. So let’s start enjoy with git-flow extensions and play with git easily. If you have any question then feel free to comment on the same 🙂 Stay tuned.

5 thoughts on “Getting started with git-flow to automate git branching workflow8 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading