Skip to content

Git Flow

Git Flow is a Git branching workflow model proposed by Vincent Driessen. It defines strict rules for different types of branches and is particularly suitable for projects with scheduled release cycles.

Git Flow Overview

What is Git Flow?

Git Flow is a branching management strategy that defines a strict branching model around project releases. This model assigns specific roles to different types of branches and defines how and when they should interact.

Benefits of Git Flow

  • 🎯 Clear Branch Structure: Every branch type has a clear purpose
  • 🚀 Parallel Development: Supports simultaneous development of multiple features
  • 🔄 Stable Release Process: Standardized release management
  • 🐛 Quick Hotfixes: Rapid response mechanism for urgent issues
  • 📈 Scalability: Suitable for large teams and complex projects

Git Flow Branch Model

Main Branches

1. main/master Branch

  • Purpose: Stable code for production environment
  • Characteristics: Only contains released code
  • Operations: Only accepts merges from release and hotfix branches
bash
# main branch should always be deployable
git checkout main
git log --oneline  # View release history

2. develop Branch

  • Purpose: Integration branch for development environment
  • Characteristics: Contains latest development changes for the next release
  • Operations: Accepts merges from feature branches
bash
# Create develop branch
git checkout -b develop main

# Or checkout from remote
git checkout -b develop origin/develop

Supporting Branches

1. Feature Branches

  • Naming Convention: feature/feature-name or feature/issue-number
  • Lifecycle: Created from develop branch, merged back to develop when complete
  • Purpose: Develop new features
bash
# Create feature branch
git checkout -b feature/user-authentication develop

# Merge after development is complete
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication

2. Release Branches

  • Naming Convention: release/version-number
  • Lifecycle: Created from develop branch, merged to main and develop when complete
  • Purpose: Prepare for new version release
bash
# Create release branch
git checkout -b release/1.2.0 develop

# Merge to main after release is complete
git checkout main
git merge --no-ff release/1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"

# Merge back to develop
git checkout develop
git merge --no-ff release/1.2.0
git branch -d release/1.2.0

3. Hotfix Branches

  • Naming Convention: hotfix/version-number or hotfix/issue-description
  • Lifecycle: Created from main branch, merged to main and develop when complete
  • Purpose: Fix urgent issues in production environment
bash
# Create hotfix branch
git checkout -b hotfix/1.2.1 main

# Merge to main after fix is complete
git checkout main
git merge --no-ff hotfix/1.2.1
git tag -a v1.2.1 -m "Hotfix version 1.2.1"

# Merge back to develop
git checkout develop
git merge --no-ff hotfix/1.2.1
git branch -d hotfix/1.2.1

Git Flow Tool Installation

Install git-flow Extension

macOS

bash
# Using Homebrew
brew install git-flow-avh

# Using MacPorts
sudo port install git-flow

Ubuntu/Debian

bash
sudo apt-get install git-flow

Windows

bash
# Using Chocolatey
choco install gitflow-avh

# Or download installer
# https://github.com/petervanderdoes/gitflow-avh/wiki/Installing-on-Windows

Verify Installation

bash
git flow version

Using Git Flow Tool

Initialize Git Flow

bash
# Initialize Git Flow in existing repository
git flow init

# Initialize with default settings
git flow init -d

Initialization process will ask for branch naming conventions:

Branch name for production releases: [main]
Branch name for "next release" development: [develop]
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []

Feature Branch Operations

Start New Feature

bash
# Create and switch to new feature branch
git flow feature start user-login

# Equivalent to
git checkout -b feature/user-login develop

Finish Feature Development

bash
# Finish feature development
git flow feature finish user-login

# This performs the following:
# 1. Merges feature/user-login into develop
# 2. Deletes feature/user-login branch
# 3. Switches back to develop branch

Publish Feature Branch

bash
# Push feature branch to remote
git flow feature publish user-login

# Pull remote feature branch
git flow feature pull origin user-login

Release Branch Operations

Start New Release

bash
# Create release branch
git flow release start 1.2.0

# Equivalent to
git checkout -b release/1.2.0 develop

Finish Release

bash
# Finish release
git flow release finish 1.2.0

# This performs the following:
# 1. Merges release/1.2.0 into main
# 2. Creates tag v1.2.0
# 3. Merges release/1.2.0 into develop
# 4. Deletes release/1.2.0 branch

Publish Release Branch

bash
# Push release branch
git flow release publish 1.2.0

Hotfix Branch Operations

Start Hotfix

bash
# Create hotfix branch
git flow hotfix start 1.2.1

# Equivalent to
git checkout -b hotfix/1.2.1 main

Finish Hotfix

bash
# Finish hotfix
git flow hotfix finish 1.2.1

# This performs the following:
# 1. Merges hotfix/1.2.1 into main
# 2. Creates tag v1.2.1
# 3. Merges hotfix/1.2.1 into develop
# 4. Deletes hotfix/1.2.1 branch

Manual Implementation of Git Flow

If you don't use the git-flow tool, you can also manually implement the Git Flow workflow:

Project Initialization

bash
# Create project
mkdir my-project
cd my-project
git init

# Create initial commit
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"

# Create develop branch
git checkout -b develop
git push -u origin develop

Feature Development Process

bash
# 1. Create feature branch from develop
git checkout develop
git pull origin develop
git checkout -b feature/user-registration

# 2. Develop feature
echo "User registration feature" > registration.js
git add registration.js
git commit -m "Add user registration feature"

echo "Registration form validation" >> registration.js
git add registration.js
git commit -m "Add registration form validation"

# 3. Push feature branch (optional, for collaboration)
git push -u origin feature/user-registration

# 4. Finish feature development, merge to develop
git checkout develop
git pull origin develop
git merge --no-ff feature/user-registration
git push origin develop

# 5. Delete feature branch
git branch -d feature/user-registration
git push origin --delete feature/user-registration

Release Process

bash
# 1. Create release branch from develop
git checkout develop
git pull origin develop
git checkout -b release/1.0.0

# 2. Release preparation (update version number, documentation, etc.)
echo "version: 1.0.0" > version.txt
git add version.txt
git commit -m "Bump version to 1.0.0"

# 3. Fix release-related bugs
echo "Fix release issue" >> bugfix.txt
git add bugfix.txt
git commit -m "Fix release related issue"

# 4. Merge to main and tag
git checkout main
git pull origin main
git merge --no-ff release/1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin main
git push origin v1.0.0

# 5. Merge back to develop
git checkout develop
git merge --no-ff release/1.0.0
git push origin develop

# 6. Delete release branch
git branch -d release/1.0.0

Hotfix Process

bash
# 1. Create hotfix branch from main
git checkout main
git pull origin main
git checkout -b hotfix/1.0.1

# 2. Fix urgent issue
echo "Fix security vulnerability" > security-fix.txt
git add security-fix.txt
git commit -m "Fix security vulnerability"

# 3. Update version number
echo "version: 1.0.1" > version.txt
git add version.txt
git commit -m "Bump version to 1.0.1"

# 4. Merge to main and tag
git checkout main
git merge --no-ff hotfix/1.0.1
git tag -a v1.0.1 -m "Hotfix version 1.0.1"
git push origin main
git push origin v1.0.1

# 5. Merge back to develop
git checkout develop
git merge --no-ff hotfix/1.0.1
git push origin develop

# 6. Delete hotfix branch
git branch -d hotfix/1.0.1

Git Flow Best Practices

1. Branch Naming Conventions

bash
# Feature branches
feature/user-authentication
feature/payment-integration
feature/issue-123

# Release branches
release/1.0.0
release/2.1.0
release/v3.0.0-beta

# Hotfix branches
hotfix/1.0.1
hotfix/security-patch
hotfix/critical-bug-fix

2. Commit Message Conventions

bash
# Feature development
git commit -m "feat: Add user authentication feature"
git commit -m "feat(auth): Implement OAuth login"

# Release preparation
git commit -m "chore: Update version to 1.2.0"
git commit -m "docs: Update CHANGELOG"

# Hotfix
git commit -m "fix: Fix login security vulnerability"
git commit -m "hotfix: Resolve memory leak issue"

3. Version Tag Conventions

bash
# Semantic Versioning
git tag -a v1.0.0 -m "Major release 1.0.0"
git tag -a v1.1.0 -m "Minor release 1.1.0"
git tag -a v1.1.1 -m "Patch release 1.1.1"

# Pre-release versions
git tag -a v2.0.0-alpha.1 -m "Alpha release 2.0.0-alpha.1"
git tag -a v2.0.0-beta.1 -m "Beta release 2.0.0-beta.1"
git tag -a v2.0.0-rc.1 -m "Release candidate 2.0.0-rc.1"

4. Code Review Process

bash
# Create Pull Request after pushing feature branch
git push -u origin feature/new-feature

# Create PR/MR in GitHub/GitLab
# Target branch: develop
# Reviewers: Team members
# Labels: feature, needs-review

# Merge after review approval
git checkout develop
git merge --no-ff feature/new-feature

Git Flow Automation

Automating with Scripts

bash
#!/bin/bash
# Git Flow Automation Script

# Start new feature
start_feature() {
    local feature_name=$1
    if [ -z "$feature_name" ]; then
        echo "Usage: start_feature <feature-name>"
        return 1
    fi
    
    git checkout develop
    git pull origin develop
    git checkout -b "feature/$feature_name"
    echo "Created feature branch: feature/$feature_name"
}

# Finish feature
finish_feature() {
    local current_branch=$(git branch --show-current)
    if [[ $current_branch != feature/* ]]; then
        echo "Error: Not on a feature branch"
        return 1
    fi
    
    local feature_name=${current_branch#feature/}
    
    git checkout develop
    git pull origin develop
    git merge --no-ff "$current_branch"
    git push origin develop
    git branch -d "$current_branch"
    
    echo "Feature branch $current_branch merged and deleted"
}

# Start release
start_release() {
    local version=$1
    if [ -z "$version" ]; then
        echo "Usage: start_release <version>"
        return 1
    fi
    
    git checkout develop
    git pull origin develop
    git checkout -b "release/$version"
    echo "Created release branch: release/$version"
}

# Finish release
finish_release() {
    local current_branch=$(git branch --show-current)
    if [[ $current_branch != release/* ]]; then
        echo "Error: Not on a release branch"
        return 1
    fi
    
    local version=${current_branch#release/}
    
    # Merge to main
    git checkout main
    git pull origin main
    git merge --no-ff "$current_branch"
    git tag -a "v$version" -m "Release version $version"
    git push origin main
    git push origin "v$version"
    
    # Merge back to develop
    git checkout develop
    git merge --no-ff "$current_branch"
    git push origin develop
    
    # Delete release branch
    git branch -d "$current_branch"
    
    echo "Release v$version completed"
}

CI/CD Integration

yaml
# .github/workflows/gitflow.yml
name: Git Flow CI/CD

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ develop ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    
    - name: Run tests
      run: |
        npm install
        npm test
    
    - name: Build
      run: npm run build

  deploy-staging:
    if: github.ref == 'refs/heads/develop'
    needs: test
    runs-on: ubuntu-latest
    steps:
    - name: Deploy to staging
      run: echo "Deploying to staging environment"

  deploy-production:
    if: github.ref == 'refs/heads/main'
    needs: test
    runs-on: ubuntu-latest
    steps:
    - name: Deploy to production
      run: echo "Deploying to production environment"

Git Flow Variants

GitHub Flow

Simplified Git Flow, suitable for continuous deployment:

bash
# 1. Create feature branch from main
git checkout -b feature/new-feature main

# 2. Develop and push
git push -u origin feature/new-feature

# 3. Create Pull Request
# 4. Code review and testing
# 5. Merge to main
# 6. Auto-deploy

GitLab Flow

Combines advantages of Git Flow and GitHub Flow:

bash
# Environment branches
main          # Production environment
pre-production # Pre-production environment
staging       # Testing environment

# Feature development
git checkout -b feature/new-feature main
# Create MR to main after development is complete

Microsoft Git Flow

Suitable for enterprise-level development:

bash
# Long-running branches
main          # Production code
develop       # Integration branch
release/*     # Release branches

# Short-lived branches
feature/*     # Feature branches
bugfix/*      # Bug fixes
hotfix/*      # Hotfixes

Troubleshooting

Common Issues

bash
# Issue 1: git-flow command does not exist
# Solution: Install git-flow extension
brew install git-flow-avh

# Issue 2: Branch merge conflict
# Solution: Manually resolve conflict
git status
git mergetool
git commit

# Issue 3: Accidentally deleted branch
# Solution: Recover from reflog
git reflog
git checkout -b recovered-branch commit_hash

# Issue 4: Release branch has unmerged features
# Solution: Merge feature to develop, then recreate release branch
git checkout develop
git merge feature/late-feature
git branch -d release/1.0.0
git flow release start 1.0.0

Branch Status Check

bash
#!/bin/bash
# Git Flow Status Check Script

echo "=== Git Flow Status Check ==="

# Check current branch
current_branch=$(git branch --show-current)
echo "Current branch: $current_branch"

# Check if main branches exist
if git show-ref --verify --quiet refs/heads/main; then
    echo "✓ main branch exists"
else
    echo "✗ main branch does not exist"
fi

if git show-ref --verify --quiet refs/heads/develop; then
    echo "✓ develop branch exists"
else
    echo "✗ develop branch does not exist"
fi

# Check active feature branches
feature_branches=$(git branch | grep "feature/" | wc -l)
echo "Active feature branches: $feature_branches"

# Check active release branches
release_branches=$(git branch | grep "release/" | wc -l)
echo "Active release branches: $release_branches"

# Check active hotfix branches
hotfix_branches=$(git branch | grep "hotfix/" | wc -l)
echo "Active hotfix branches: $hotfix_branches"

# Check unmerged branches
echo ""
echo "=== Branches not merged to develop ==="
git branch --no-merged develop | grep -v main

echo ""
echo "=== Branches not merged to main ==="
git branch --no-merged main | grep -v develop

Summary

Key points of Git Flow:

Branch Structure

main/master     # Production branch
develop         # Development branch
feature/*       # Feature branches
release/*       # Release branches
hotfix/*        # Hotfix branches

Workflow

  • 🚀 Feature Development: feature → develop
  • 📦 Version Release: develop → release → main + develop
  • 🔥 Hotfix: main → hotfix → main + develop

Applicable Scenarios

  • ✅ Scheduled release cycles
  • ✅ Need to maintain multiple versions
  • ✅ Large team collaboration
  • ✅ Strict quality control

Tool Selection

  • git-flow extension: Automated operations
  • Manual implementation: Better control
  • GUI tools: Visual management
  • CI/CD integration: Automated deployment

After mastering Git Flow, you will be able to:

  • 📋 Establish standardized development processes
  • 🔄 Support parallel feature development
  • 🚀 Manage stable release cycles
  • 🛠️ Quickly respond to urgent issues

In the next chapter, we will learn about Git undo and restore operations.

Content is for learning and research only.