Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: February 6, 2024
In this tutorial, we’ll learn about the trunk-based development approach in Version Control Systems (VCS).
First, we’ll explore the general concept, see how it’s different from the feature branch development model, the common characteristics involved, and the workflow to achieve it.
Then, we’ll discuss the different factors to consider while going for trunk-based development. Finally, we’ll see the pros and cons of using trunk-based development.
Trunk-based development is a source-control branching model, where the developers work on a single branch called ‘trunk’ or ‘mainline’ (‘master’ or ‘main’ in Git).
Since there’s no long-lived development branch in this model, merge conflicts are generally avoided, and the builds aren’t broken frequently.
In the trunk-based development approach, the developers mainly collaborate on the ‘trunk’. However, larger teams may use short-lived branches that can last no more than a couple of days.
Developers commit the code very often to push the latest copy in the trunk. Subsequently, the builds trigger automated tests, which help to minimize the integration issues at a later stage.
As we can see, this approach encourages continuous integration and works best for small agile teams.
Feature branch-based development is the most common approach the teams chose to practice. In this decentralized version control approach, the developers create long-lived dedicated branches from the main codebase to isolate the changes needed for a particular feature/improvement.
Once the code is ready and tested, the commit will be made to the respective branch. Finally, these branches will be merged back to the main code base after resolving the merge conflicts.
Here, the idea is to enable developers to work independently and commit the changes without affecting other developers or teams. Specifically, this can be useful in relatively large projects with multiple contributors and/or complex codebases.
The trunk-based development approach has some common characteristics. These factors influence how effectively the developers work and collaborate between themselves. Let’s see them in brief.
In trunk-based development, all the developers work on a single branch (master/trunk). This enables faster code integrations, thereby reducing merge conflicts.
The developers integrate the code changes as frequently as they can instead of waiting for a large chunk of new code.
Integrating small but frequent commits regularly avoids the risk of doing a big merge at a later stage. Subsequently, the merges will be much easier with relatively fewer conflicts.
Also, as the commits are more regular, the chance of introducing bugs is minimal. Moreover, it’s much easier to identify and resolve merge conflicts.
Test automation is an essential feature in the trunk-based development model. Since there are regular commits, we need to make sure that the builds aren’t broken. In addition, the new commits shouldn’t introduce regressions.
Increasing the test coverage helps to reduce the risk of regression and helps in fixing the identified issues quickly.
Moreover, these automated tests provide instant feedback so that the issues are resolved faster, which results in improved code quality.
For each new commit, other developers review and provide feedback before merging the code to the main branch. This helps to improve the code quality and identify potential issues in advance. The timeframe to complete the code review needs to be defined so that it doesn’t delay the commits.
Any branch that is longer than two days may end up becoming a long-lived branch. In trunk-based development, long-lived branches are avoided to prevent merge conflicts and allow faster integration.
Let’s see the major steps involved in the trunk-based development model.
In the first step, once the code is ready, the developer commits the code directly to the trunk or a short-lived branch that was created from the trunk. Here, the rule of thumb is that everyone commits to the mainline every day.
More precisely, we should never have more than a day’s work sitting unintegrated in the local repository.
As soon as the code is committed, the developer creates a pull request to merge the new code with the main branch.
The pull request would contain the basic description of the change, the list of code changes, and any other relevant information.
In this step, other developers review the code changes from the pull request. This step helps to identify the defects which could have escaped from the eyes of the original author.
Further, having this 4-eyes validation for the new code enforces the best practices for new developers.
Trunk-based development relies heavily on automated tests. These tests include unit and integration tests that are triggered automatically when a pull request is created.
Automated tests prevent the new code changes from breaking the existing functionalities. More importantly, it gives the confidence that the new code works fine without affecting the old code.
After passing through the peer review and automated tests, the new code is ready for merge with the trunk. Since there’s no long-lived branch and the commit interval is short, we minimize the risk of merge conflicts.
Multiple factors have influence when choosing between trunk-based development and feature branch development.
Specifically, we should take care of the specific needs of the project and the development team involved. Both these development approaches have their advantages and disadvantages. However, the primary factors to consider include the complexity of the codebase, the actual development process, and the number of developers.
Let’s see what are the major pros and cons of trunk-based development.
In this article, we had an overview of the usage of trunk-based development in modern software development.
Also, we discussed briefly the characteristics and workflow of the trunk-based development model. While on the subject, we also compared the differences between branch-based and trunk-based development.
Finally, we saw the benefits and drawbacks of using the trunk-based development. To conclude, trunk-based development is worth considering for teams that want to streamline their development process, improve team collaboration, and reduce technical debt.