Lesson 2 of 4 Branching Strategies

Designing a Branch Strategy

This lesson compares trunk-based development, feature branches and release branches.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the practices that make trunk-based development safe.
  • Explain the risk of long-lived branches.
  • Explain when release branches are justified.
  • Choose a strategy from stated requirements.

Trunk-based development

In trunk-based development, all developers integrate small changes into a single branch, called main or trunk, at least once a day. Research from DORA associates this practice with high delivery performance, because integration problems appear at once, when they are small.

Three practices make it safe. Short-lived branches, lasting hours or a day or two, are used for review, or small teams commit directly. Automated tests run for every change, so main stays healthy. Feature flags hide unfinished work, which means that a half-built feature can be merged without being exposed to users. Without those practices, trunk-based development quickly breaks main.

Select all that apply

What makes trunk-based development safe?

Exam skills 2.1.1

Pick the practices that keep main healthy when everyone integrates daily.

Log in to try this activity Interactive activities are free with an account.

Feature branches

A feature branch holds the work for one change until it is ready, then merges through a pull request. It gives each change a place for isolated review and testing. Its main risk is duration. A feature branch that lasts for six weeks diverges from main, so the merge is large and risky, and nobody tests the combination of all in-flight work. In practice, the same pattern is safe when branches last a day or two, which is really GitHub Flow, and hazardous when they last for weeks.

Fork-based workflows extend this idea for open-source projects, or for contributors who should not have write access to the main repository. A contributor works in a personal copy, and submits a pull request back to the original repository.

Release branches

A release branch is created from main when a version is ready to stabilize, for example release/2.3. Only fixes go onto it, and new development continues on main. It is needed when a team must support released software that it cannot simply replace, such as installed desktop software, or a product where customers remain on different versions.

The direction in which fixes travel matters. Microsoft recommends the release flow approach, in which a fix is made first in main and then cherry-picked to the release branch. This guarantees that the fix cannot be lost in the next release. A team that fixes only the release branch must remember to bring the change back, and the defect may reappear.

Put in order

A fix under release flow

Exam skills 2.1.1

Arrange how a defect found in version 2.3 is fixed under release flow.

Log in to try this activity Interactive activities are free with an account.

Other named workflows

GitFlow uses long-lived main and develop branches, along with feature, release and hotfix branches. It suits scheduled releases of versioned products, but its number of branches slows integration, so it is often unsuitable for continuous delivery. On the exam, a team that struggles with merges and wants to deploy often should move away from it.

Environment branches, with one branch for each of test, staging and production, are a related pattern that causes drift, because code on each branch can differ. Modern practice is to build one artifact and promote it through environments, as a later lesson explains.

True or false

GitFlow and environment branches

Exam skills 2.1.1

Check when these named workflows help or hurt.

Log in to try this activity Interactive activities are free with an account.

Choosing a strategy

The following table summarizes how requirements point to a strategy. These strategies can be combined: a team might use trunk-based development on main and add release branches only for versions that need support.

SituationRecommended approach
Frequent deployments of a single web serviceTrunk-based development or GitHub Flow
Long-running work that cannot be released yetTrunk-based development with feature flags
Several supported versions in productionAdd release branches, with fixes made on main first
Untrusted or external contributorsFork-based pull requests
Painful merges from long-lived branchesShorter branches and more frequent integration

The following diagram compares sprint-long feature branches with the recommended short-lived branches.

From sprint-long feature branches to short-lived branches. Long-lived branches made merges take two days and surfaced defects late. Integrating at least daily, with feature flags hiding incomplete work and automated tests on each pull request, keeps main healthy; a develop branch or release branches would not help here.
The scenario is illustrative.
Scenario questions

Matching strategy to situation

Exam skills 2.1.1

Choose the branch strategy that fits each team.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reducing merge pain

A team of 12 developers keeps a feature branch open for each sprint-long feature. Merges at the end of each sprint take two days, and defects often appear only after merging. The product is a single web application deployed to one environment. What should you recommend?

The team should move to short-lived branches or trunk-based development, integrating at least daily, and use feature flags so that incomplete work can reach main safely. Automated tests on each pull request keep main healthy. Adding a develop branch, as in GitFlow, would add another integration stage without addressing the long-lived branches. Release branches are not justified, as only one version is in production.

Check yourself

Under release flow, where is a fix made first?

Show answer

In main, and it is then cherry-picked to the release branch. This guarantees that the fix reaches future releases.

Exam Tips

Remember for the exam

  • Trunk-based development: integrate at least daily, with feature flags and tests.
  • A long-lived feature branch is the usual cause of painful merges.
  • Release branches are justified by several supported versions.
  • Make a fix in main first, then cherry-pick it to the release branch.
  • GitFlow suits scheduled versioned releases more than continuous delivery.