No history yet

Advanced Git Branching

Advanced Branching Strategies

Once you're comfortable with basic branching and merging, it's time to think bigger. When you work on a team, you need a shared process for managing code. Without a plan, collaboration can quickly become chaotic, with broken builds and conflicting changes. A branching strategy is a set of rules that brings order to this process.

A branching strategy is a set of rules or guidelines that development teams use to manage the process of writing, merging, and deploying code with the help of a version control system like Git.

These strategies provide a roadmap for how to use branches for new features, bug fixes, and releases. They define which branches to use, when to create them, and how they should be merged. Let's explore three popular models: Gitflow, GitHub Flow, and Trunk-Based Development.

The Gitflow Model

Gitflow is a structured branching model designed for projects with scheduled release cycles. It uses two long-lived branches and several types of temporary, supporting branches to organize the development process. It's robust, but also the most complex of the strategies we'll cover.

The two main branches are:

  • main: This branch stores the official release history. It's always stable and ready for production. Code is only merged into main from release branches or hotfix branches.
  • develop: This is the integration branch for new features. All feature branches are created from develop and merged back into it. This branch contains the latest development changes for the next release.

Gitflow also uses three types of supporting branches:

  • Feature branches are for developing new features. They branch off from develop and are merged back into develop when complete.
  • Release branches are for preparing a new production release. They branch from develop, allow for last-minute bug fixes and documentation, and are merged into both main and develop when ready.
  • Hotfix branches are for fixing critical bugs in a production version. They branch from main, and once the fix is complete, they are merged back into both main and develop.
Branch TypeBranches FromMerges IntoPurpose
main-release, hotfixOfficial release history
developmainfeature, release, hotfixIntegration for next release
featuredevelopdevelopDevelop new features
releasedevelopmain, developPrepare for a new release
hotfixmainmain, developFix urgent production bugs

Gitflow's main advantage is its strict separation of concerns, which makes it ideal for managing multiple versions of a product in production. However, its complexity can be overkill for simpler projects and can slow down teams practicing continuous delivery.

GitHub Flow

GitHub Flow is a much simpler, lightweight alternative to Gitflow. It's designed for teams that practice continuous deployment, where changes are rolled out to production frequently.

The core principle is that the main branch is always deployable. There's only one main branch, and anything in it is considered stable and ready to be released.

The GitHub Flow workflow is straightforward:

  1. Create a descriptive branch off main for any new work.
  2. Add commits to that branch locally and push them to the remote repository.
  3. Open a pull request to start a discussion and code review.
  4. Once the pull request is approved and passes automated tests, it's merged into main.
  5. The change is then immediately deployed from main.

This model is excellent for web applications and other projects where you can deploy to production multiple times a day. Its simplicity makes it easy for teams to learn and use. However, it's less suitable for projects that need to support multiple versions in production, like a desktop application or a mobile app with release schedules dictated by app stores.

Trunk-Based Development

Trunk-Based Development (TBD) is a strategy where all developers work on a single branch, called the "trunk" (usually main or master). Instead of long-lived feature branches, developers merge small, frequent changes directly into the trunk.

Trunk-Based Development is a branching strategy where developers work on a single branch, typically main, and commit changes frequently.

How do you manage incomplete features? TBD relies heavily on "feature flags" (or feature toggles). An unfinished feature is wrapped in a conditional block in the code, allowing it to be present in the codebase but disabled in production. Once the feature is complete, the flag is flipped to enable it for users.

This approach avoids the complex merge conflicts that can arise from long-running branches. By integrating code continuously, everyone stays up-to-date with the latest changes, promoting a high-level of collaboration. TBD is a cornerstone of Continuous Integration and Continuous Delivery (CI/CD).

Choosing the Right Strategy

There is no single best branching strategy. The right choice depends on your team, your project, and your release process.

  • Choose Gitflow if: You have a product with explicit, scheduled releases (e.g., monthly or quarterly). It's also good for open source projects or when you need to support multiple versions of your software in production.

  • Choose GitHub Flow if: You practice continuous deployment and release to production multiple times a day. It's ideal for web apps and services where you can quickly roll back a bad deployment.

  • Choose Trunk-Based Development if: You are a mature DevOps team practicing CI/CD. It requires discipline, a robust automated test suite, and the use of feature flags.

Consider your team's size, experience with Git, and the nature of your project. Don't be afraid to start simple and adapt your strategy as your team and project evolve.

Quiz Questions 1/6

What is the primary purpose of a branching strategy in a team environment?

Quiz Questions 2/6

In the Gitflow model, which branch serves as the primary integration branch for new features?

By understanding these different models, you can choose a workflow that enhances collaboration and makes your development process smoother and more predictable.