Git Branching Strategies: How To Manage Your Codebase Effectively
Tags: git, gitflow, devops, trunkbaseddevelopment
Published: 2023-09-13 06:27:13

In modern software and DevOps teams, version control systems are the powerhouse when it comes to delivering software. they are capable of managing source code changes and tracking code change history. As much as version control systems are effective, working on big teams with simultaneous activity can make it difficult to create branches or even merge code. This is why we need a branching strategy.  

> **_A branching strategy is used by software development teams to design a proper way in which branches are created and code is merged._** 

## Prerequisites
The only requirement before reading this article is that you have a fair understanding of version control, git and branching in git.

## Sub-topics

- Why should you implement a branching strategy

- Factors to Consider When Choosing a Branching Strategy.

- Types Of Branching Strategies.
  

## Why should you implement a branching strategy?
 
A branching strategy is essential for any large-scale project as it improves collaboration and accelerates the delivery process and business value. Distributed version control systems such as git gives you the flexibility to use different branching strategies, unlike centralized systems. 

## Factors to Consider When Choosing Branching Strategy? 
The branch strategy you use will mainly be influenced by the project requirements. When picking a strategy, ask yourself the following questions:

- How big is the size of the team?
- What is the complexity and size of the project?
- How often do you make deployments?
- Will your development workflow fit in with the strategy? 

In the following section, I will cover the different types of strategies that are commonly used. Remember that whichever strategy you choose it is not fixed, and you can refine it as your project needs change.  

## Types of Branching Strategies.
We have two most commonly used branching strategies namely, feature branching and trunk-based development. There are other types as well such as gitflow, github flow, release branching and more. 

## Feature-branching
 Feature branching method requires the developer to create a new feature branch for every new feature and bugs. In this way you can isolate your work in progress from your completed work in the main branch. This technique makes it easy to access and review the history of changes made after writing commits and opening pull requests. Here's how to implement this workflow:
 

1. Identify and assign features for developers to work on. Consider if the developers can work on these features independently with minimal conflict.

2. Let each developer create a feature branch based off the main/master branch. Developers can now work on their respective features in the branches they created without interference from others.

3. Implement code review in your workflow whenever a pull request or merge request is made.

4. After code review and merge is done, the feature branch should be deleted to keep the repository clean.

5. Repeat the process for each feature. as best practice, use a  [naming convention]("https://learn.microsoft.com/en-us/azure/devops/repos/git/git-branching-guidance?view=azure-devops#name-your-feature-branches-by-convention") for your feature branches.

![feature branching diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t4o5tfdp75czgcjigy6.png)

## Trunk-Based Development
This branching strategy enforces continuous integration where changes can be released frequently, and automated tests run after each commit. Trunk based development encourages developers to merge changes to the trunk (master) as often as possible thus eliminating the need for long lived branches that can cause merge conflicts.

#### Best Practices 

1. Small incremental changes - commits should be made daily which requires short lived branches. When teams break down work into smaller sections it makes it easier to commit daily and review quickly. 
2. Feature toggles - by creating feature flags, developers can deactivate unfinished features and still make changes then activate the feature when done.
3. Automated testing and code reviews - implement a code review system where committed changes are reviewed as fast as possible. In any modern software project, regular testing is part of the continuous integration process. It helps teams to discover and resolve bugs early, which can lead to technical debt.
4. Delete inactive branches or once a branch is merged. Ensure that there are no more than three branches in your repository.

Trunk based strategy is suitable for any team size and complex projects. Its success will solely depend on how it is implemented and if developers embrace it. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pt6kkf9u7i2dn8cclbp.png)

## Gitflow Branching Strategy
Gitflow strategy is a way for developers to work in parallel and away from the master branch by using feature branches. This workflow consists of five different branches.

- Master branch: This is the main branch that is created at the start of the project. Commits made in this branch are tagged with a version number. 

- Develop: This branch is also a master branch where changes from the feature branch are integrated. Feature branches are branched off the develop branch.

- Feature branch: Feature branches are where developers create and develop new features.

- Release - This branch is created off the develop branch when you are ready to release new features.

- Hotfix - used for emergency bug fixes.

Gitflow is most suitable for software that are explicitly versioned to allow for roll backs. All versions are in production which means they need continuous support. This strategy can be used when developing operating systems or packages but not web applications.