Logo

dev-resources.site

for different kinds of informations.

Git branching strategies

Published at
1/11/2025
Categories
git
discuss
changelog
devops
Author
Vivesh
Categories
4 categories in total
git
open
discuss
open
changelog
open
devops
open
Git branching strategies

Git branching strategies are techniques to organize and manage feature development, collaboration, and version control in a project. Choosing the right strategy depends on the team size, project requirements, and deployment needs. Below are the most common Git branching strategies:

1. Git Flow

A robust and popular strategy for projects with scheduled release cycles.

Branches:

  • Main (Master): Contains the stable, production-ready code.
  • Develop: Serves as the integration branch for features; this is where the next release is prepared.
  • Feature Branches: Created for individual features or tasks, branched off develop.
  • Release Branches: Created to prepare for a release, branched off develop.
  • Hotfix Branches: Used to fix critical issues in production, branched off main.

Flow:

  1. Feature branches are merged into develop after completion.
  2. When develop is stable, a release branch is created, tested, and merged into main.
  3. Critical fixes are applied via hotfix branches directly on main and backported to develop.

Use Cases:

  • Projects with scheduled releases.
  • Complex projects with multiple developers.

2. GitHub Flow

A simpler branching model commonly used for continuous deployment.

Branches:

  • Main: The only permanent branch that contains production-ready code.
  • Feature Branches: Created for individual features or bug fixes, branched off main.

Flow:

  1. Work on a feature branch.
  2. Open a pull request (PR) when ready to merge into main.
  3. After code review and CI/CD checks, merge the feature branch into main.
  4. Deploy directly from main.

Use Cases:

  • Small teams.
  • Projects requiring frequent deployments.

3. GitLab Flow

A flexible strategy that combines elements of Git Flow and GitHub Flow. It works well with CI/CD pipelines.

Branches:

  • Main (or Production): Stable, production-ready code.
  • Environment-Specific Branches: Optional branches like staging or pre-production.
  • Feature Branches: For new features or bug fixes, branched off main.

Flow:

  1. Develop features in feature branches.
  2. Merge changes into the appropriate environment branch (e.g., staging for testing).
  3. Deploy from the environment branch to production.

Use Cases:

  • Teams with multiple environments (e.g., dev, staging, prod).
  • Projects requiring manual or staged deployments.

4. Trunk-Based Development

A minimalist strategy emphasizing simplicity and rapid integration.

Branches:

  • Main (Trunk): The only long-lived branch.
  • Short-Lived Feature Branches: Optional branches for new features, often merged back within hours or days.

Flow:

  1. Developers commit directly to main or use short-lived branches.
  2. Use automated testing to ensure stability.
  3. Frequent merges keep the trunk branch up to date.

Use Cases:

  • Agile teams practicing continuous integration (CI).
  • Projects with rapid development cycles.

5. Release Flow

A Microsoft-driven branching strategy designed for large-scale software releases.

Branches:

  • Main: Stable branch for production.
  • Feature Branches: For feature development.
  • Release Branches: Used to prepare and stabilize a specific release.

Flow:

  1. Work on features in feature branches.
  2. Merge completed features into main.
  3. Create a release branch for final testing, stabilization, and deployment.

Use Cases:

  • Enterprise projects with strict release schedules.

Comparison Table of Strategies

Strategy Complexity Deployment Frequency Ideal for
Git Flow High Scheduled Complex, large teams
GitHub Flow Low Continuous Simple, small teams
GitLab Flow Medium Flexible Teams with environments
Trunk-Based Development Low Continuous Agile, fast-moving teams
Release Flow Medium Scheduled Enterprise projects

Best Practices for Branching

  1. Use Descriptive Branch Names: Use names like feature/login, bugfix/header-issue, or hotfix/payment-fix for clarity.
  2. Code Reviews: Always use pull/merge requests to ensure code quality and maintain collaboration.
  3. Automated Testing: Integrate CI/CD pipelines to automatically test and validate changes before merging.
  4. Merge Regularly: Keep branches up to date with main to avoid large, complex merge conflicts.
  5. Delete Stale Branches: Remove branches after merging to keep the repository clean.

Task: Create a branching strategy for your team’s projects.

Here’s a proposed branching strategy for your team’s projects, designed to balance efficiency, collaboration, and code stability. This strategy assumes your team works in an Agile environment and frequently delivers updates.

Branching Strategy Overview

We will adopt a combination of GitLab Flow and Trunk-Based Development, tailored for the team's needs. This approach works well with CI/CD pipelines and multiple environments (e.g., development, staging, and production).

Branches

  1. main (or production):

    • Always contains stable, production-ready code.
    • Used for deployments to production.
    • Protected branch: Only pull requests (PRs) can merge into main, requiring approvals and CI checks.
  2. develop (optional if you need a dedicated integration branch):

    • Used for integrating completed features and testing them before they go to main.
    • For teams that prefer a staging phase before production.
  3. Feature Branches:

    • Created for developing new features, enhancements, or bug fixes.
    • Naming convention: feature/<feature-name> or bugfix/<bug-description>.
    • Merged into main or develop after code review and testing.
  4. Hotfix Branches:

    • Used for fixing critical issues in production.
    • Naming convention: hotfix/<issue-description>.
    • Branched off main and merged back into both main and develop (or relevant feature branches).
  5. Environment Branches (optional):

    • For managing specific environments like staging or qa.
    • staging is used for testing final changes before merging into main.

Flow of Work

Feature Development

  1. Developer creates a branch from main (or develop if using a develop branch).
   git checkout -b feature/<feature-name> main
  1. Developer works on the feature, commits changes, and pushes to the remote branch.
   git push origin feature/<feature-name>
  1. Once complete, open a pull request (PR) to main or develop for review.
  2. After approval and successful CI tests, the feature branch is merged.
  3. Delete the feature branch after merging to keep the repository clean.

Releases

  1. When ready for a release, create a release branch (if needed):
   git checkout -b release/<version> develop
  1. Perform final testing on the release branch.
  2. Fix any bugs directly on the release branch.
  3. Merge the release branch into main and tag the release version:
   git tag -a v1.0 -m "Release version 1.0"
   git push origin --tags
  1. Optionally merge the release branch back into develop to keep it updated.

Hotfixes

  1. Create a hotfix branch from main:
   git checkout -b hotfix/<description> main
  1. Fix the issue and push changes.
  2. Merge the hotfix branch into main and deploy to production:
   git checkout main
   git merge hotfix/<description>
   git push origin main
  1. Backport the fix to develop to ensure consistency.

Naming Conventions

  • feature/<feature-name>: New features or enhancements.
  • bugfix/<bug-description>: Non-critical bug fixes.
  • hotfix/<issue-description>: Critical fixes for production issues.
  • release/<version>: Release-specific branches.
  • staging, qa, or dev: Environment-specific branches.

CI/CD Integration

  1. Automated Testing:

    • All branches should trigger automated tests in CI/CD pipelines.
    • Ensure tests pass before allowing merges into main or develop.
  2. Code Reviews:

    • Require at least one reviewer for PRs.
    • Use code review tools to maintain quality and ensure adherence to coding standards.
  3. Deployment:

    • Automatically deploy the main branch to production after CI/CD checks pass.
    • Deploy the staging branch to a staging environment for testing.

Best Practices

  1. Small, Frequent Merges: Merge changes frequently to avoid large, complex conflicts.
  2. Enforce Protected Branches: Protect main and develop to ensure no direct commits.
  3. Delete Merged Branches: Keep the repository clean by deleting merged branches.
  4. Use Pull Requests: Always create PRs for code review and testing.
  5. Document Changes: Update documentation or changelogs as part of the release process.

Visual Workflow Example

main
  |
  |--------- hotfix/<description>
  |
  |--------- release/<version>
  |                 |
develop ------------|
  |       \-------- feature/<feature-name>
  |       \-------- bugfix/<description>
  |

Happy Learning !!!

Featured ones: