Git Flow
Git Flow is a branching model designed by Vincent Driessen that provides a structured way of managing Git branches in a project. It introduces a set of conventions for creating and merging branches, enabling teams to handle feature development, releases, and hotfixes in a controlled and efficient manner.
Table of Content
Why Use Git Flow?
Git Flow offers a clear workflow that helps teams:
- Manage Releases: By clearly separating branches for development, releases, and hotfixes.
- Improve Collaboration: Provides a common structure that everyone on the team can follow.
- Reduce Merge Conflicts: Organized branching reduces the risk of conflicts and keeps the project history clean.
Setting Up Git Flow
Installing Git Flow
Git Flow is available as an extension of Git. To install Git Flow:
- On macOS:
brew install git-flow
- On Windows: Use Git for Windows SDK or install via package managers like Chocolatey:
choco install gitflow-avh
- On Linux: Install using your package manager (e.g., apt, yum):
sudo apt-get install git-flow
Initializing Git Flow in a Repository
Step 1: Navigate to Your Repository
Open your terminal and navigate to the root of your Git repository.
Step 2: Initialize Git Flow
git flow init
This command will prompt you to set up the main and develop branches, and configure prefixes for feature, release, and hotfix branches.
Understanding Git Flow Branches
Main Branches: main and develop
- main Branch: Represents the production-ready state of your code. Only stable and fully tested code should be merged here.
- develop Branch: Serves as the integration branch for features. This is where the latest development efforts are merged and tested.
Supporting Branches: Feature, Release, and Hotfix
- Feature Branches: Used to develop new features. Created from develop and merged back into develop when complete.
- Release Branches: Created from develop when preparing for a new release. Allows for final tweaks, testing, and documentation before merging into main.
- Hotfix Branches: Created from main to quickly address critical issues in the production version. Once fixed, merged back into both main and develop.
Working with Git Flow
Creating and Merging Feature Branches
Step 1: Create a Feature Branch:
git flow feature start <feature-name>
This command creates a new branch from develop named feature /<feature-name>.
Step 2: Develop Your Feature
Commit your changes as you work on the feature.
Step 3: Finish the Feature:
git flow feature finish <feature-name>
This merges the feature branch back into develop and deletes the feature branch.
Creating Release Branches
Step 1: Start a Release Branch:
git flow release start <version>
This command creates a release branch from develop.
Step 2: Prepare the Release
Make any final changes, such as version bumping and documentation updates.
Finish the Release:
git flow release finish <version>
This merges the release branch into both main and develop, tags the commit on main, and deletes the release branch.
Handling Hotfixes
Step 1: Start a Hotfix
git flow hotfix start <version>
This creates a hotfix branch from main.
Step 2: Fix the Issue
Commit the necessary fixes.
Step 3: Finish the Hotfix
git flow hotfix finish <version>
This merges the hotfix into both main and develop, tags the commit on main, and deletes the hotfix branch.
Advanced Features
Configuring Git Flow
- Custom Branch Names: During initialization, you can configure branch names and prefixes to suit your team’s preferences.
- Scripts and Hooks: Automate tasks like code formatting, testing, or deployment during the finish steps of feature, release, and hotfix branches.
Using Git Flow with Pull Requests
Combine Git Flow with pull requests to enhance code review and collaboration:
- Use pull requests to merge feature branches into develop or release branches into main.
- Ensure code quality and consistency by using CI/CD checks within pull requests.
Automating Git Flow in CI/CD Pipelines
- CI/CD Integration: Set up automated testing and deployments triggered by merges into main or develop.
- Release Automation: Automate tagging and deployment processes when finishing release or hotfix branches.
Common Issues and Troubleshooting
Resolving Merge Conflicts
Merge conflicts can arise when finishing branches:
- Identify Conflicts: Git will prompt you with conflicted files during merges.
- Resolve Conflicts: Use your preferred tool to resolve conflicts, commit the changes, and continue the merge.
Dealing with Diverged Branches
If branches diverge significantly, you may need to rebase or perform manual merges:
- Rebase Feature Branches: Keep feature branches up-to-date with develop to minimize conflicts.
Handling Incorrect Branching
If a branch is started incorrectly, you can delete the branch and start over:
git flow feature delete <feature-name>
Alternatives to Git Flow
- GitHub Flow: A simpler model that uses a single main branch for all development. Feature branches are created off main and merged back via pull requests.
- GitLab Flow: GitLab Flow extends GitHub Flow by integrating environment-specific branches (e.g., staging, production).
- Trunk-Based Development: A fast-paced model where developers work directly on a single branch (trunk), often used in conjunction with feature flags.