Trunk Based Development
Trunk Based Development

Git Branching Strategies – Trunk Based Development

In previous articles, we have talked about Gitflow workflow, GitHub flow and GitLab flow. In this last article of the series, we are going to discuss about Trunk Based Development.

What is Trunk Based Development ?

Trunk Based Development is a branching strategy in which developers collaborate via a single branch, the master (or main) branch. Unlike Gitflow (or even GitLab flow) There are generally no other long-lived branches. The master (or main) branch is generally referred to as trunk.

Developers can create short-lived feature branches. The branches are created for specific backlog item. Or sometimes more than one branches for one backlog item. This is to ensure frequent updates to the trunk. Once the development is complete, the feature branches are merged back to main branch. If the teams are smaller, they may also decide to commit directly to the trunk (similar to what happens in centralized version control systems).

Feature Flags

This branching strategy assumes that main branch is always ready for deployment / release. This means there should not be half baked code in the main branch. But on the other hand, the strategy also recommends to have frequent and small commits (or merges) to the main branch. How can both be possible ? The answer is feature flags.

The code portions which are not ready yet for release are wrapped inside feature flags. Thus, half baked or not ready code portions are hidden (and not executed) after deployment.

Feature toggles also come handy in case the newly released features are not behaving as expected. There is no need of roll back and feature flag can be set again to disable the new feature and enable the older behavior.

Pros and Cons

Trunk based development is not a new style and it has already been adopted widely in many organizations. One of the important benefit that this style provides is – no merge hells.

The work from feature branches is frequently getting merged to the main. Hence there is no need of spending time in merging activities. There are no log lived branches other than main. Hence, there is no need for merging, just for releases / deployments.

Trunk based development is required practice for continuous integration. It allows continuous stream of commits to the main branch. Adding automated build and automated tests help to validate the code correctness and code quality. This ultimately helps in continuous integration and quicker releases. It also reduces the number of code freezes (ideally to zero).

In addition to these benefits, there is one thing of caution. The developers have to be cautious to ensure that the trunk is always in a state which can be released. The developers should always be alert about which features should need feature flags and what should be the default states of those flags.

Further Reading

If you want you can refer the articles given below which explain this branching strategy further:

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply