Agile3 .COM

Trunk-Based Development

Trunk-Based Development (TBD) is a source-code management practice where developers merge small, frequent updates directly into a single shared branch, often called the "trunk" or "main" branch. It stands in contrast to development models that rely on long-lived feature branches. TBD is a cornerstone practice for achieving Continuous Integration (CI) and Continuous Delivery (CD), enabling teams to integrate code changes multiple times a day. By minimizing the time code spends in isolation, TBD significantly reduces merge conflicts, accelerates feedback loops, and fosters a more collaborative and agile development environment. It is a fundamental engineering practice within the wider Agile knowledge graph, directly supporting rapid iteration and high-quality software delivery.

What is Trunk-Based Development?

Trunk-Based Development (TBD) is a version control branching strategy where all developers commit their changes directly to a single, shared main branch, known as the "trunk" or "main." The core principle is to keep the trunk in a constantly releasable state by integrating changes frequently and in small increments. This approach minimizes divergence between developer workspaces and the main codebase, drastically reducing the complexity and pain associated with merging large, long-lived feature branches.

Historically, software development often involved developers working on isolated feature branches for extended periods, sometimes weeks or even months. This led to significant "merge hell" when attempting to integrate these disparate branches back into the main codebase, often resulting in complex conflict resolution, broken builds, and delayed releases. The advent of Agile methodologies and the increasing demand for faster delivery cycles highlighted the inefficiencies of such approaches.

TBD emerged as a response to these challenges, gaining prominence with the rise of Continuous Integration (CI) in the early 2000s. Martin Fowler, a prominent figure in Agile and software architecture, has been a strong advocate, emphasizing its role in enabling rapid, reliable software delivery. The practice is deeply intertwined with the principles of DevOps, which advocate for automating and streamlining the entire software delivery pipeline, from code commit to deployment.

The primary purpose of TBD is to facilitate continuous integration, ensuring that the codebase is always in a working, tested state. By committing small changes frequently, developers receive immediate feedback on the impact of their code, allowing them to detect and fix integration issues early. This proactive approach prevents the accumulation of technical debt related to integration and makes the codebase more stable and predictable. It also directly supports Continuous Delivery (CD) and Continuous Deployment, as a constantly releasable trunk is a prerequisite for automated deployments.

TBD is important because it fosters a culture of collaboration and shared ownership (Collective Code Ownership). Developers are encouraged to communicate and coordinate their work more closely, as their changes directly impact the shared trunk. It reduces the cognitive load associated with managing complex branch structures and allows teams to focus more on delivering value. Furthermore, it simplifies the process of Code Reviews, as smaller changes are easier and quicker to review, leading to higher quality feedback and faster approval cycles.

The practice is foundational for modern software engineering teams aiming for high velocity and reliability. It integrates seamlessly with other Agile Engineering Practices such as Test Driven Development (TDD), Automated Testing, and Refactoring, all of which contribute to maintaining a healthy and robust trunk. Without TBD, achieving true continuous integration and delivery becomes significantly more challenging, if not impossible, for many teams.

How It Works

Trunk-Based Development operates on a straightforward yet disciplined workflow, centered around the principle of frequent, small integrations into a single main branch.

Workflow

  1. Pull Latest Trunk: Before starting any new work, a developer pulls the absolute latest version of the trunk to ensure their local environment is up-to-date. This minimizes the chance of working on stale code and encountering conflicts later.
  2. Make Small, Atomic Changes: Developers work on very small, focused tasks. Each change should ideally address a single concern or implement a small part of a feature. The goal is to keep the scope of each commit minimal.
  3. Run Local Tests: Before committing, developers run relevant local tests (e.g., Unit Testing, Integration Testing) to ensure their changes haven't introduced immediate regressions or broken existing functionality.
  4. Commit to Trunk: Once local tests pass and the developer is confident in their changes, they commit directly to the trunk. This commit should be accompanied by a clear, descriptive message.
  5. Automated Build and Test Pipeline: Immediately after a commit, a Continuous Integration (CI) pipeline is triggered. This pipeline automatically builds the application, runs a comprehensive suite of Automated Tests (unit, integration, end-to-end), and performs static code analysis.
  6. Immediate Feedback and Fixes: If the CI pipeline fails (e.g., tests fail, build breaks), the team is immediately notified. The highest priority for the developer (or the team) is to fix the broken trunk as quickly as possible. This "fix forward" mentality is crucial to maintaining a healthy trunk.
  7. Feature Flags for Incomplete Work: For larger features that cannot be completed in a single small commit, Feature Flags are used. This allows developers to commit incomplete code to the trunk without exposing it to users. The feature remains "dark" until it's fully developed, tested, and ready for release, at which point the flag is toggled on.
  8. Code Reviews: While direct commits to trunk are common, Code Reviews can still be integrated. This might involve reviewing changes *before* committing (e.g., Pair Programming, Mob Programming) or reviewing changes *after* they've been committed to the trunk, with an understanding that any issues found must be fixed immediately.

Principles

  • Single Source of Truth: The trunk is always the definitive, most up-to-date version of the codebase.
  • Frequent Integration: Developers integrate their work into the trunk multiple times a day, not just once a week or sprint.
  • Small Batches: Changes are kept small and focused to minimize the risk of conflicts and simplify debugging.
  • Automated Verification: A robust suite of automated tests and a reliable CI pipeline are non-negotiable.
  • Immediate Remediation: Broken builds or failing tests on the trunk are treated as critical issues requiring immediate attention.
  • Releasable at Any Time: The trunk should always be in a state where it could theoretically be deployed to production, even if not all features are visible (thanks to feature flags).

Key Concepts

The Trunk (Main Branch)

The central, single source of truth for the codebase. All development activity converges here. In Trunk-Based Development, the goal is to keep this branch perpetually stable and releasable. Developers commit directly to it, or use very short-lived feature branches that are merged back within hours, not days. This contrasts sharply with models like Gitflow, which maintain multiple long-lived branches.

Short-Lived Branches

While direct commits to the trunk are ideal, some teams use very short-lived feature branches (often called "task branches"). These branches typically exist for only a few hours, or at most a day, before being merged back into the trunk. The key is that they are never allowed to diverge significantly from the trunk, ensuring frequent integration and minimizing merge conflicts.

Continuous Integration (CI)

CI is fundamental to TBD. Every commit to the trunk (or a short-lived branch) automatically triggers an automated build and test process. This immediate feedback loop is crucial for detecting integration issues, build failures, and test regressions as soon as they occur, allowing developers to fix them quickly before they escalate.

Feature Flags (Feature Toggles)

Feature Flags are a critical enabler for TBD, especially for larger features. They allow developers to commit incomplete or experimental code to the trunk without impacting end-users. The feature is wrapped in a conditional statement that can be toggled on or off, controlling its visibility and behavior in different environments or for specific user groups. This allows continuous integration without continuous deployment of unfinished features.

Automated Testing

A comprehensive suite of Automated Tests (Unit Testing, Integration Testing, End-to-End Testing) is essential. These tests provide the safety net that allows developers to commit frequently to the trunk with confidence. Without robust automated testing, the risk of introducing regressions and breaking the trunk becomes unacceptably high, undermining the benefits of TBD.

Small, Frequent Commits

The practice of making small, atomic changes and committing them frequently to the trunk is central to TBD. This minimizes the amount of code that needs to be merged at any one time, drastically reducing the likelihood and complexity of merge conflicts. It also makes it easier to pinpoint the source of any issues that arise.

Practical Considerations

Benefits

  • Reduced Merge Conflicts: By integrating frequently and in small increments, the time between merges is minimized, significantly reducing the chances of complex and time-consuming merge conflicts.
  • Faster Feedback Loops: Developers receive immediate feedback from the CI pipeline on the health of their changes, allowing for quick detection and resolution of issues.
  • Improved Code Quality: Frequent integration, coupled with robust automated testing and often Code Reviews, leads to a higher quality, more stable codebase.
  • Enhanced Collaboration: TBD encourages closer collaboration and communication among team members as they are constantly working on a shared, evolving codebase.
  • Supports Continuous Delivery/Deployment: A constantly releasable trunk is a prerequisite for effective Continuous Delivery and Continuous Deployment, enabling faster time-to-market.
  • Simplified Branch Management: Eliminates the complexity of managing multiple long-lived feature branches, release branches, and their associated merging strategies.
  • Easier Rollbacks: Since changes are small and frequent, identifying and reverting a problematic commit is generally simpler and less disruptive.

Limitations

  • Requires Strong Discipline: Teams must be highly disciplined about committing small changes, running tests, and fixing broken builds immediately.
  • Heavy Reliance on Automated Testing: Without a comprehensive and reliable suite of Automated Tests, TBD can lead to a frequently broken trunk, negating its benefits.
  • Initial Setup Overhead: Establishing a robust CI pipeline and extensive automated test coverage requires an initial investment of time and effort.
  • Can Be Challenging for Large Features: While Feature Flags mitigate this, very large, architectural changes might still require careful planning and coordination to avoid destabilizing the trunk.
  • Potential for Instability: If discipline wanes or tests are insufficient, the trunk can become unstable, impacting the productivity of the entire team.

Common Mistakes

  • Large, Infrequent Commits: Defeats the purpose of TBD by reintroducing merge conflict risks and delaying feedback.
  • Ignoring CI Failures: Allowing the trunk to remain broken for extended periods is a critical failure that halts team progress.
  • Insufficient Automated Testing: Committing code without adequate test coverage is a recipe for introducing regressions and instability.
  • Misusing Feature Flags: Over-reliance on flags for long-term branching or not cleaning them up after use can add complexity.
  • Lack of Communication: Without close coordination, developers might inadvertently step on each other's toes, even with small commits.
  • Not Refactoring Regularly: Allowing technical debt to accumulate makes it harder to make small, clean changes.

Best Practices

  • Commit Small and Often: Aim for multiple commits per day, each addressing a single, atomic change.
  • Prioritize Automated Testing: Invest heavily in Unit Testing, Integration Testing, and End-to-End Testing to build a strong safety net.
  • Maintain a Fast and Reliable CI Pipeline: Builds and tests should run quickly to provide rapid feedback.
  • "Fix Forward" Mentality: When the trunk breaks, drop everything and fix it immediately.
  • Utilize Feature Flags: Employ Feature Flags to manage incomplete features and enable continuous integration without exposing unfinished work.
  • Practice Code Reviews: Integrate practices like Pair Programming, Mob Programming, or asynchronous Code Reviews to maintain quality and share knowledge.
  • Refactor Continuously: Regularly Refactor the codebase to keep it clean, maintainable, and easy to extend with small changes.
  • Monitor Trunk Health: Use dashboards and alerts to keep track of build status, test pass rates, and other key metrics.

Real-world Examples

Many high-performing technology companies, including Google, Amazon, and Microsoft, extensively use variations of Trunk-Based Development. Google, for instance, operates on a massive monorepo where thousands of engineers commit to a single trunk daily, relying on sophisticated tooling, robust automated testing, and a strong culture of immediate fixes to maintain stability. This approach allows them to deploy changes rapidly and frequently, supporting their continuous innovation cycles. Smaller startups and agile teams also adopt TBD to achieve similar benefits of speed and reliability, often leveraging cloud-based CI/CD platforms to automate their pipelines.

Frequently Asked Questions

Q: What is the main difference between Trunk-Based Development and Gitflow?
A: TBD uses a single main branch for all development, with frequent, small commits. Gitflow uses multiple long-lived branches (e.g., develop, feature, release) which are merged less frequently, leading to more complex merge operations.
Q: Can I use feature branches with Trunk-Based Development?
A: Yes, but they should be very short-lived (hours, not days) and merged back into the trunk frequently. The goal is to minimize divergence from the main branch.
Q: How do we handle incomplete features in TBD?
A: Feature Flags are the primary mechanism. Incomplete code is committed to the trunk but hidden behind a flag, preventing it from being exposed to users until it's ready.
Q: What if a commit breaks the trunk?
A: The highest priority is to fix the trunk immediately. This often means the developer who introduced the breakage, or the team, stops other work to resolve the issue, either by fixing forward or reverting the problematic commit.
Q: Is Trunk-Based Development only for large teams?
A: No, TBD is beneficial for teams of all sizes. While large organizations like Google use it, smaller teams can also gain significant advantages in terms of speed, stability, and reduced merge pain.
Q: Does TBD replace Code Reviews?
A: No, Code Reviews are still crucial. They can be done before committing (e.g., Pair Programming) or after committing to the trunk, focusing on smaller, more manageable changes.

Explore Related Topics

References & Further Reading

  • Fowler, Martin. "TrunkBasedDevelopment." martinfowler.com.
  • Humble, Jez, and Farley, David. "Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation." Addison-Wesley Professional, 2010.
  • Forsgren, Nicole, Humble, Jez, and Kim, Gene. "Accelerate: The Science of Lean Software and DevOps: Building and Scaling High Performing Technology Organizations." IT Revolution Press, 2018.
  • Google Cloud. "Trunk-based development." cloud.google.com.
  • Atlassian. "Trunk-based development." atlassian.com.
© 2026 Agile3 . All rights reserved.