Branching Strategies¶
โ Back to Git
๐๏ธ GitFlow Branching Strategy¶
This is a common Git workflow (often referred to as GitFlow or a simplified variant of it) designed to provide a robust framework for managing releases and parallel development. It is primarily used for software development projects where multiple developers collaborate on features that need to be explicitly versioned and released.
Core Branches¶
main: The source of truth for your production-ready code. Commits here should only come from merges fromdevelopor release branches, and they are usually tagged with release versions.develop: The integration branch for features. It represents the latest delivered development changes for the next release.feature/*: Short-lived branches used to develop new features, branching off fromdevelop.release/*: Branches used to prepare for a new production release. They branch off fromdevelopand merge into bothmainanddevelop.
Workflow¶
- Feature Development:
- Create a new
feature/*branch branching off fromdevelop. - Developers write code, test locally, and commit changes to their isolated feature branch.
- Create a new
- Merge to Develop:
- Once the feature is complete, open a Pull Request (PR) against the
developbranch. - Upon approval, the PR is merged into
developand the feature branch is deleted.
- Once the feature is complete, open a Pull Request (PR) against the
- Release Preparation:
- When
develophas accumulated enough features for a release, arelease/*branch (e.g.,release/v1.0.0) is created fromdevelop. - Only bug fixes, documentation generation, and other release-oriented tasks are allowed on this branch.
- When
- Release to Main:
- Once the release branch is ready, it is merged into
mainand tagged with a version number. - The release branch is also merged back into
developto ensure that any bug fixes made during the release process are kept. - At this point, CI/CD can deploy the latest
mainbranch to the production environment.
- Once the release branch is ready, it is merged into
Flow Diagram¶
main *------------------------------------------------------------------------------------> * (v1.0.0)
\ /
release \ *------------------------> *
\ / \
develop *-------> *----------------> *-------------> *-------> *---------------------------> *
\ / \ /
feature-login *----> *-----> * \ /
\ /
feature-cart *----> *
๐ Environment-Based Branching for CI/CD¶
Another common pattern, especially when managing infrastructure, declarative environments, or Continuous Delivery (CD) deployments, is mapping branches directly to specific environments.
The Strategy¶
You maintain three primary branches representing your environments:
- dev: The Development environment.
- qa: The Quality Assurance / Testing environment.
- prod: The Production environment.
Version Configuration¶
Each branch contains a standard configuration file specific to its environment. Using independent files avoids merge conflicts between branches that may carry different histories side-by-side.
- dev branch uses versions-dev.yaml
- qa branch uses versions-qa.yaml
- prod branch uses versions-prod.yaml
Example: versions-dev.yaml
app1: 1.0.0
app2: 0.0.2
The CD Pipeline Flow¶
-
Deploying to
dev:- The CD pipeline triggered on the
devbranch reads theversions-dev.yamlfile from thedevbranch. - It reads
app1: 1.0.0andapp2: 0.0.2and deploys these versions to the Development environment.
- The CD pipeline triggered on the
-
Deploying to
qa:- For QA deployments, the pipeline uses the
qabranch. - It reads
versions-qa.yamlfrom theqabranch and heavily tests these versions in the QA environment.
- For QA deployments, the pipeline uses the
-
Deploying to
prod:- For Production deployments, the pipeline uses the
prodbranch. - It ensures that only carefully vetted configurations in the
prodbranch'sversions-prod.yamlare deployed to Production.
- For Production deployments, the pipeline uses the
Updating Versions via Pull Requests¶
Because each environment's state is strictly gated by its branch, all updates to the environment's version file must go through a Pull Request process.
- Create an Update Branch: To deploy a new version to QA, create a temporary branch off
qa(e.g.,update-qa-app1-1.0.1). - Modify the File: Update
versions-qa.yamlin this new branch to setapp1: 1.0.1. - Open a PR: Open a Pull Request merging your update branch into the
qabranch. - Review & Approve: The change is reviewed and approved by peers or release managers.
- Merge: Upon merge, the target branch (
qa) is updated. - Trigger Deployment: The CD pipeline detects the merge on the
qabranch and automatically applies the newversions-qa.yamlstate to the QA environment.
Flow Diagrams¶
Development Environment
update-dev * (PR: update versions-dev.yaml)
/ \
dev *-------------------> * (Deploys app to Dev)
QA Environment
update-qa * (PR: update versions-qa.yaml)
/ \
qa *-------------------> * (Deploys app to QA)
Production Environment
update-prod * (PR: update versions-prod.yaml)
/ \
prod *-------------------> * (Deploys app to Prod)
Best Practice
Keep Branches Isolated: The key advantage of this strategy is that dev, qa, and prod never merge into one another. You only merge configuration update branches into their respective environments. This drastically reduces merge conflicts and accidental deployments of untested code.
Infrastructure as Code (IaC)
This pattern is overwhelmingly popular for GitOps workflows (like ArgoCD or Flux) and Infrastructure as Code repositories (like Terraform or Helm charts) where the codebase primarily consists of declarative state rather than application source code.
๐ง Quick Quiz โ Branching Strategies¶
What is the primary purpose of the develop branch in the GitFlow strategy?
In an Environment-Based CI/CD strategy, how do you correctly deploy a new version to the QA environment?
Why is maintaining independent version files (like versions-dev.yaml, versions-qa.yaml) beneficial in an environment-based branching strategy?
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here