Git & GitHub

Git Flow Workflow

13 min Lesson 24 of 35

Git Flow Workflow

Git Flow is a popular branching model designed by Vincent Driessen that provides a robust framework for managing larger projects with scheduled releases. It defines a strict branching structure around project releases.

What is Git Flow?

Git Flow is a branching strategy that uses multiple long-lived branches and specific branch types for different purposes. It's particularly suited for projects with scheduled release cycles.

Origin: Git Flow was introduced in 2010 by Vincent Driessen in his blog post "A successful Git branching model" and has become one of the most widely adopted workflows.

Core Branches

Git Flow uses two main branches that have infinite lifetime:

main (or master): - Production-ready code - Every commit is a new release - Always stable and deployable - Never commit directly to main develop: - Integration branch for features - Contains latest development changes - Source for next release - Feature branches merge here

Supporting Branches

Git Flow uses three types of supporting branches with limited lifetime:

Feature Branches: - Branch from: develop - Merge back to: develop - Naming: feature/*, feature-* - Purpose: Develop new features Release Branches: - Branch from: develop - Merge back to: main AND develop - Naming: release/*, release-* - Purpose: Prepare production release Hotfix Branches: - Branch from: main - Merge back to: main AND develop - Naming: hotfix/*, hotfix-* - Purpose: Quick production fixes

Git Flow Visualization

Here's how the branches interact:

main o-----------o-----------o \ /|\ / release \ / | \ / o-----o | o-----o \ / | \ / develop o-o----o----o-o----o | | | feature o o o \ / \ / o o

Installing Git Flow

Install Git Flow extension for convenient commands:

# macOS (Homebrew) brew install git-flow # Linux (Ubuntu/Debian) apt-get install git-flow # Windows # Download from: https://gitforwindows.org/ # Git Flow is included in Git for Windows # Verify installation git flow version
Note: You can use Git Flow without the extension by using regular Git commands, but the extension makes it more convenient.

Initializing Git Flow

Set up Git Flow in your repository:

# Initialize Git Flow (interactive setup) git flow init # Answer the prompts (or press Enter for defaults): # Branch name for production releases: [main] # Branch name for "next release" development: [develop] # Feature branches prefix: [feature/] # Release branches prefix: [release/] # Hotfix branches prefix: [hotfix/] # Support branches prefix: [support/] # Version tag prefix: [] # Or use defaults non-interactively git flow init -d

Working with Feature Branches

Develop new features in isolated branches:

# Start a new feature git flow feature start user-authentication # This creates feature/user-authentication from develop # and switches to it automatically # Work on your feature git add . git commit -m "Add login functionality" git commit -m "Add logout functionality" # Finish the feature (merges back to develop) git flow feature finish user-authentication # Behind the scenes, this does: # 1. Merges feature into develop # 2. Deletes the feature branch # 3. Switches back to develop
# Publish feature to remote (for collaboration) git flow feature publish user-authentication # Pull someone else's feature git flow feature pull origin user-authentication # List all features git flow feature list

Creating a Release

Prepare a new production release:

# Start a release branch git flow release start 1.2.0 # This creates release/1.2.0 from develop # Only bug fixes, documentation, and release tasks allowed # Make release preparations echo "1.2.0" > VERSION git commit -am "Bump version to 1.2.0" # Update CHANGELOG git commit -am "Update CHANGELOG for 1.2.0" # Finish the release git flow release finish 1.2.0 # This will: # 1. Merge release into main # 2. Tag the release with version # 3. Merge release back into develop # 4. Delete the release branch # Push everything including tags git push origin main develop --tags
Important: No new features should be added during the release branch. Only bug fixes and release-related changes are allowed.

Handling Hotfixes

Quickly fix critical production bugs:

# Start a hotfix (from main) git flow hotfix start 1.2.1 # This creates hotfix/1.2.1 from main # Fix the critical bug git commit -am "Fix critical security vulnerability" # Finish the hotfix git flow hotfix finish 1.2.1 # This will: # 1. Merge hotfix into main # 2. Tag with hotfix version # 3. Merge hotfix back into develop # 4. Delete the hotfix branch # Push changes git push origin main develop --tags

Git Flow Without the Extension

Use regular Git commands if you prefer:

Feature Branch: git checkout develop git checkout -b feature/my-feature # Work... git checkout develop git merge --no-ff feature/my-feature git branch -d feature/my-feature Release Branch: git checkout develop git checkout -b release/1.2.0 # Prepare release... git checkout main git merge --no-ff release/1.2.0 git tag -a v1.2.0 -m "Version 1.2.0" git checkout develop git merge --no-ff release/1.2.0 git branch -d release/1.2.0 Hotfix Branch: git checkout main git checkout -b hotfix/1.2.1 # Fix bug... git checkout main git merge --no-ff hotfix/1.2.1 git tag -a v1.2.1 -m "Version 1.2.1" git checkout develop git merge --no-ff hotfix/1.2.1 git branch -d hotfix/1.2.1
The --no-ff Flag: Forces creation of a merge commit even when fast-forward is possible. This preserves the historical existence of the feature branch.

Semantic Versioning

Git Flow works well with semantic versioning:

Format: MAJOR.MINOR.PATCH (e.g., 2.4.1) MAJOR (2): - Incompatible API changes - Breaking changes - Example: 1.9.0 → 2.0.0 MINOR (4): - New features (backward compatible) - Functionality additions - Example: 2.3.0 → 2.4.0 PATCH (1): - Bug fixes (backward compatible) - Hotfixes - Example: 2.4.0 → 2.4.1 Examples: Feature release: 1.2.0 → 1.3.0 Hotfix: 1.3.0 → 1.3.1 Breaking change: 1.9.0 → 2.0.0

When to Use Git Flow

Git Flow is ideal for certain project types:

✓ Good For: - Scheduled releases (monthly, quarterly) - Multiple versions in production - Large teams with defined roles - Projects requiring stable releases - Desktop or mobile apps - Traditional software products ✗ Not Ideal For: - Continuous deployment (deploy multiple times daily) - Small teams with fast iterations - Web applications with rolling releases - Projects needing immediate production updates - SaaS products with continuous delivery

Git Flow Best Practices

✓ Keep main always deployable ✓ Develop should be stable (all tests pass) ✓ One feature per feature branch ✓ Small, focused feature branches ✓ Regular merges to avoid conflicts ✓ Delete branches after finishing ✓ Use semantic versioning for tags ✓ Document release notes ✓ Test thoroughly on release branch ✓ Never commit directly to main or develop

Git Flow Commands Cheat Sheet

Initialization: git flow init [-d] Features: git flow feature start <name> git flow feature finish <name> git flow feature publish <name> git flow feature pull origin <name> Releases: git flow release start <version> git flow release finish <version> git flow release publish <version> Hotfixes: git flow hotfix start <version> git flow hotfix finish <version> Other: git flow feature list git flow release list git flow hotfix list

Integrating with Pull Requests

Combine Git Flow with GitHub Pull Requests:

# Instead of finishing feature locally: git flow feature start my-feature # Work on feature... git push -u origin feature/my-feature # Create Pull Request on GitHub: # - Base: develop # - Compare: feature/my-feature # After PR is approved and merged: git checkout develop git pull git branch -d feature/my-feature # Local cleanup

Practical Exercise:

Task: Practice the Git Flow workflow

  1. Initialize a new Git repository
  2. Set up Git Flow: git flow init -d
  3. Create a feature: git flow feature start add-navbar
  4. Make changes and commit them
  5. Finish the feature: git flow feature finish add-navbar
  6. Start a release: git flow release start 1.0.0
  7. Update version file and commit
  8. Finish the release: git flow release finish 1.0.0
  9. Create a hotfix: git flow hotfix start 1.0.1
  10. Fix a bug and finish: git flow hotfix finish 1.0.1
  11. View your branch structure: git log --all --graph --oneline

Expected Outcome: You'll understand the complete Git Flow lifecycle from feature development to production releases and hotfixes.

Common Pitfalls

Problem: Forgetting to merge hotfix to develop Solution: Git Flow does this automatically; manual merges need care Problem: Long-lived feature branches Solution: Keep features small; merge frequently to develop Problem: Conflicts when finishing release Solution: Merge develop into release branch before finishing Problem: Committing directly to main Solution: Use branch protection rules on GitHub Problem: Too many branches Solution: Finish and delete branches promptly

Git Flow vs Other Workflows

Git Flow: + Structured and organized + Clear release management - Complex for beginners - Overhead for continuous deployment GitHub Flow: + Simple and straightforward + Great for continuous deployment - Less structure for releases - Not ideal for multiple versions Trunk-Based Development: + Very fast iterations + Minimal branching - Requires discipline - Feature flags needed

Summary

In this lesson, you learned:

  • Git Flow is a branching model with defined structure
  • Two main branches: main (production) and develop (integration)
  • Three supporting branches: feature, release, and hotfix
  • Feature branches for new development
  • Release branches for preparing production releases
  • Hotfix branches for critical production fixes
  • Git Flow extension simplifies workflow commands
  • Works well with semantic versioning
  • Best for scheduled releases and traditional software
  • Can be combined with Pull Requests for code review
Next Up: In the next lesson, we'll explore GitHub Flow, a simpler workflow designed for continuous deployment!