Code branching
Branches are independent copies of code that stem from a central codebase, referred to as the trunk, or main. Code branching allows multiple team members to work independently in their own branch.
Ideally, branches are short-lived, making it easier to merge code into the trunk more often. The ability to frequently and automatically merge branches is essential for continuous integration.
Frequent merging of code branches is encouraged in trunk-based development to reduce the risk of breaking the build. All branches should be merged at least once every 24-hour period to ensure the stability of the trunk.
Continuous integration
Integration is the practice of bringing together the different elements of the system to verify that everything works together as intended before deploying to production.
Continuous integration (CI) extends this idea to integrate and test the system after every change. It is possible to test changes in an integrated environment when using multiple branches.
However, until the changes are merged into the trunk and tested together, there is a risk that conflicts will cause things to break. Trunk-based development encourages smaller changes and more frequent integration, making it less likely to cause problems than merges from long-lived branches.
When development teams frequently merge branches into the trunk
- They improve the flow of work.
- Reduce the chances of instability.
- Help ensure the codebase is always available for release.
Continuous integration is a necessary part of continuous delivery.
Continuous delivery
Continuous delivery is the practice of ensuring the codebase is always in a releasable state. Broken software is unacceptable. Continuous delivery enables teams to release changes on demand, with a high degree of confidence.
Continuous delivery allows all kinds of changes to be put into production safely, quickly, and in a sustainable way. This includes experiments, bug fixes and new features.
Trunk-based development supports continuous delivery by encouraging smaller changes to be merged into the trunk more often. This means:
- Large features are broken down into small incremental changes.
- Testing is performed more often.
- Problems are identified much more quickly.
Gitflow
Gitflow, is a popular branching model that emerged in 2010. It supports multiple development scenarios, resulting in the need to maintain several branches that are simultaneously in use.
Gitflow, however, can quickly become complicated with long-lived feature branches and multiple primary branches. Feature branches can involve multiple developers and days or weeks of work, all of which contribute to “merge hell”.
It is difficult to introduce automation, which slows deployment. It also removes autonomy from the development team to merge changes.
Gitflow is in direct conflict with the goals of continuous delivery and maximised flow.
Trunk based development
Trunk-based development is a way to control source code regardless of how many people are working in a development team. It allows the whole team to share the same codebase while continually integrating changes. This helps improve delivery flow and results in lower cost, higher quality code.
Trunk-based development benefits from automated testing to allow small branches to be merged, more frequently.
Scaled trunk-based development is recommended for teams of all sizes. It requires the use of short-lived branches flowing through pair programming or pull request code reviews and automated tests before merging into the trunk.
What good looks like
Development teams are able to iterate quickly, implement CI/CD and accelerate release cadence. Automated integration testing and code coverage testing are triggered when new code is merged into the trunk.
Automation is essential to ensure a steady stream of commits flows into the main branch with minimal conflict.
Trunk-based development creates a single source of codebase truth. A trunk-based development model enables:
- Reduction in the volume of code under development
- Continuous code integration
- Fast, efficient code reviews
- Frequent deployment of code to production
- Confidence in the stability of the main branch
- On-demand release of the codebase.