Monday, 24 March 2025

Git Branching Strategies

     A Git branching strategy is a set of guidelines that defines how branches should be created, named, and managed in a Git repository to ensure a smooth and organized development workflow. The goal is to make it easier to collaborate with multiple developers, track features, and release code efficiently while avoiding conflicts and managing different stages of development (e.g., new features, bug fixes, and production-ready code). 

Here are some common Git branching strategies: 

  • Trunk-Based Development Strategy 
  • Feature-based strategy 
  • Gitflow strategy 

  • Trunk-Based Development Strategy

     In trunk-based development, developers work directly in the main branch (often called trunk). Instead of maintaining multiple long-lived branches, developers focus on small, incremental changes and frequent merges to the main branch. This strategy promotes continuous integration and rapid deployment. 

  • main or trunk: This is the only active branch. Developers create very short-lived feature branches or work directly on main, making small, frequent commits. 
Trunk based git branching strategy

Workflow: 

  1. Developers work on small features and commit changes directly to main. 
  2. Feature branches are short-lived (often only a few hours or days), reducing merge conflicts. 
  3. Continuous integration tools automatically build and test changes. 

  •  Feature-based strategy

Feature branching is a simple branching strategy where each new feature is developed on its own branch. This approach allows for isolated development and testing of features, making it easier to roll back changes if necessary. Feature branches are created off the master branch, and when the feature is complete, it is merged back into the master branch. 

Feature branching is a great option for smaller projects or teams that want a straightforward branching strategy. 

  1. main (or master): Contains production-ready code. 
  2. Environment branches or Develop branch: These branches represent different stages of deployment, such as staging, production, or testing. 
  3. Feature branches: Work is done on individual feature branches, which are merged into environment branch (or develop branch) for testing and deployment. 
Feature based git branching strategy


Workflow: 
  1. Feature branches are created from the main or staging branch (or develop). 
  2. After development, the feature branch is merged into an environment or develop branch for testing. 
  3. Once tested, the code is merged into main (production). 

 

  • Gitflow branching strategy 

Gitflow is a popular branching strategy that uses two main branches: master and develop. 

The master branch contains production-ready code, while the develop branch contains the latest development code. Feature branches are created off the develop branch, and when the feature is complete, it is merged back into the develop branch. When the develop branch is ready for release, it is merged into the master branch, and a new release is created. 

Gitflow is a robust branching strategy that works well for large projects with long release cycles, but it can be complicated to set up and maintain. It also requires strict discipline around feature branch creation and merging. 

gitflow branching strategy


Workflow: 

  1. Start with develop for ongoing development. 
  2. Create feature branches for new functionality. 
  3. Merge feature branches into develop when completed. 
  4. Use release branches for preparing stable versions. 
  5. Fix critical issues on hotfix branches and merge back into both master and develop. 
Thank you.