Git is the most popular version control tool that gives software developers power to track, manage, organize and share their code. Git is an open source tool which enables users to collaborate their work and manage the changes efficiently. Therefore, teams need to have a process in place to merge multiple changes at once, reducing number of conflicts.
What is a Branch?
In Git, a Branch is essentially a pointer to latest commit in a given context. As we create new commits in a new branch, Git creates new pointers to track the changes. Git branches can then be seen as a pointer to snapshot of the changes.
What is a Branching strategy?
Branching is a key feature of Git that allows developers to create multiple branches, work on them and then merge them back to the main branch. A Branching strategy is essentially a set of rules that teams adopt while writing, merging and deploying their code using a version control tool. Merging code is one of the most hectic task when the whole team is working on a code, a good branching strategy makes this task easier and keeps the code clean to avoid any code errors.
Guidelines for creating Branches
Use feature branches for your work
Use a consistent naming convention while naming the branches, name should be able to clearly define the purpose of the branch.
Use feature flags to manage long running branches. Feature flags are if statements in the code base that helps team to turn feature on or off.
Review and merge code using pull requests. Pull requests let others know about the changes you have pushed to a branch. Once reviewed, the changes can be merged.
Keep a high quality and up to date 'main' branch
Create release branches for releases
Use tags for releases
Use proper naming convention for deployments
Below are some most commonly used Branching strategies -
1. Git Flow
'main' and 'develop' are two main branches in this branching strategy.
'main' branch contains production-ready code while 'develop' branch will have the latest development code.
'feature' branches are created from 'develop' branch for each feature that needs to be worked on. Three types of branches for 'feature' can be 'feature', 'release' and 'hotfix' as per the purpose.
After development and testing is finished for a feature, the branches are merged back to the 'develop' branch. '
develop' branch is merged into 'main' branch when it is tested and ready to be deployed.
Pros
Various types of branches make it easy to organize the work.
Works very well for large projects with long release cycles.
Systematic development process with efficient testing.
'release' branches allow for easy and continuous support for multiple versions of production code.
Cons
With increase in size and complexity, it could slow down the development process.
Not a good choice when going for CI/CD.
2. Feature Branching
Also known as Github Flow branching strategy, this is a simple one in which every new feature is developed on its own branch.
'main' branch is the stable production-ready branch.
'feature' branches are created from the 'main' branch for developing new features. No 'release' branches.
Once the feature is complete after testing, it is merged back into 'main' branch.
Pros
Fast and streamlined branching suitable for short production cycles and frequent releases, focussing Agile.
Quick identification and resolution of issues.
Suitable for CI/CD.
Cons
Suitable for small projects and small teams.
Not suitable to have multiple versions of code in production at same time.
Lack of 'develop' branch make it susceptible to more bugs.
3. Release Branching
Gitlab Flow or Release Branching strategy involves creating new branch for each release of the project. 'release' branch is named as per the deployment environments, like production, pre-production.
'main' branch is the stable production-ready branch.
'feature' branches are created from the 'main' branch for developing new features.
Once the feature is complete after testing, it is merged back into 'main' branch.
'release' branches are created from the 'main' branch for final testing before merging into 'main' for production.
Pros
Good option for projects with short release cycles.
With some modification, suitable for CI/CD.
Cons
Challenging to handle multiple 'release' branches at the same time.
Lack of structure can lead to messy collaboration.
4. Trunk based development
In this branching strategy, developers work on a single branch ie mostly the 'main' branch and use feature flags to isolate new features until they are ready for release.
'main' branch is used for development where all new features are added and tested using feature flags.
when a feature is ready to be release, feature flags are removed.
hotfixes go directly into 'main' branch.
Pros
Enhances collaboration with better visibility to changes.
Greatly suitable for CI/CD, since changes are done more frequently.
Less conflicts.
Cons
Requires frequent merges.
Suitable for experienced developers.
Use of feature flags can make some features get missed for release sometimes.
Choosing best suitable Branching Strategy
Product Type and Release Method | Team Size | Collaboration Maturity | Branch mode |
---|---|---|---|
All | Small | High | Trunk Based development |
Products supporting CI/CD | Medium | Moderate | Feature based or Trunk based development |
Products with definite release window and periodic version release | Medium | Moderate | Feature based or Release based development |
Products that are demanding for high quality and support CI/CD | Medium | Moderate | Release based development |
Products demanding high quality and have a long maintenance for released versions | Large | Moderate | Git Flow |
Conclusion
Using a Branching strategy is a key while using Github for project collaboration. It provides a process and makes merges easier with lesser conflicts. There is no perfect strategy and strategy to choose will depend on project and team. Also, a team can start with one strategy and change it later too as per the requirement of the project and team comfort. Whatever strategy is chosen, it should aim to increase team productivity by providing a clear way to organize work.
Commenti