Introduction

Git Flow is a branching model for Git, created by Vincent Driessen. It provides a robust framework for managing larger projects and is particularly well-suited for projects with a scheduled release cycle. Git Flow defines a strict branching model designed around the project release. This model is widely used in the industry and helps in maintaining a clean and organized repository.

Key Concepts of Git Flow

Git Flow introduces several key concepts and branches:

  • Master Branch: This branch contains the production-ready code. Every commit to this branch should be a release.
  • Develop Branch: This branch contains the latest delivered development changes for the next release. It is the integration branch for features.
  • Feature Branches: These branches are used to develop new features for the upcoming or a distant future release.
  • Release Branches: These branches support preparation of a new production release. They allow for minor bug fixes and preparing meta-data for a release.
  • Hotfix Branches: These branches are used to quickly patch production releases.

Git Flow Branching Model

The Git Flow branching model can be visualized as follows:

Branch Type Branch Name Purpose
Master master Contains production-ready code.
Develop develop Integration branch for features.
Feature Branch feature/* Used to develop new features.
Release Branch release/* Supports preparation of a new production release.
Hotfix Branch hotfix/* Used to quickly patch production releases.

Setting Up Git Flow

To use Git Flow, you need to install the Git Flow extension. Here’s how you can set it up:

Installation

For macOS:

brew install git-flow

For Linux:

sudo apt-get install git-flow

For Windows: Download and install from Git for Windows.

Initializing Git Flow

Once installed, you can initialize Git Flow in your repository:

git flow init

This command will prompt you to set up the branch names. The default names are usually sufficient.

Working with Git Flow

Creating a Feature Branch

To start working on a new feature:

git flow feature start <feature-name>

This command creates a new branch from develop named feature/<feature-name>.

Finishing a Feature Branch

Once the feature is complete, you can finish it:

git flow feature finish <feature-name>

This command merges the feature branch back into develop and deletes the feature branch.

Creating a Release Branch

When you are ready to prepare a new release:

git flow release start <release-version>

This command creates a new branch from develop named release/<release-version>.

Finishing a Release Branch

To finish the release:

git flow release finish <release-version>

This command merges the release branch into both master and develop, tags the release, and deletes the release branch.

Creating a Hotfix Branch

To quickly fix an issue in production:

git flow hotfix start <hotfix-name>

This command creates a new branch from master named hotfix/<hotfix-name>.

Finishing a Hotfix Branch

To finish the hotfix:

git flow hotfix finish <hotfix-name>

This command merges the hotfix branch into both master and develop, tags the hotfix, and deletes the hotfix branch.

Practical Example

Here’s a practical example of using Git Flow:

  1. Initialize Git Flow:

    git flow init
    
  2. Start a Feature:

    git flow feature start new-feature
    
  3. Work on the Feature:

    # Make changes and commit
    git add .
    git commit -m "Add new feature"
    
  4. Finish the Feature:

    git flow feature finish new-feature
    
  5. Start a Release:

    git flow release start 1.0.0
    
  6. Finish the Release:

    git flow release finish 1.0.0
    
  7. Start a Hotfix:

    git flow hotfix start fix-bug
    
  8. Finish the Hotfix:

    git flow hotfix finish fix-bug
    

Summary

Git Flow provides a structured branching model that helps in managing larger projects with multiple developers. By using feature, release, and hotfix branches, it ensures that the codebase remains clean and organized. This workflow is particularly useful for projects with a scheduled release cycle, providing a clear path from development to production.

In the next topic, we will explore the GitHub Flow, a simpler alternative to Git Flow that is well-suited for continuous deployment.

© Copyright 2024. All rights reserved