Lesson 7 of 8 in Git and collaboration

Branching strategies and trade-offs

Advanced11 minWritten for Git 2.28 or newer

A branching strategy is the team's agreement about which branches exist, how long they live and how changes reach production. Git does not enforce one, so the choice shapes how often you meet conflicts and how easily you can ship a fix.

Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.

Read these first: Remotes, fetch, push and pull requests, Resolving merge conflicts

The problem this solves

Git allows any branch layout, so teams invent their own, and many copy a layout built for a very different product. A mobile app that ships versions to app stores and a web service deployed many times a day have different needs.

The layout decides how long changes wait before meeting each other. The longer a branch lives, the more other work lands underneath it and the larger the eventual merge conflict. The layout also decides how you patch what is running in production without shipping everything else on main.

No strategy is free. Each one moves effort from one place to another, and the useful skill is knowing what you are paying.

How it works

Trunk-based flow: small changes into main often
Trunk-based flow: small changes into main oftenA short branch goes through a pull request with checks into main, the pipeline ships main, and unfinished work ships hidden behind a feature flag.1 open PR2 merge3 shipships behind itShort branchsmall, merged soonPR and checksreview plus testsmainalways releasablePipelinebuilds and deploysFeature flaghides unfinished work
  • Runs code
  • Control
  • Data
  • Security

A short branch goes through a pull request with checks into main, the pipeline ships main, and unfinished work ships hidden behind a feature flag.

  1. Short branch to PR and checks: 1 open PR
  2. PR and checks to main: 2 merge
  3. main to Pipeline: 3 ship
  4. Pipeline to Feature flag: ships behind it
Release branch: patch what is running
Release branch: patch what is runningA release branch is cut from main and deployed as a tagged version; a fix made on main is cherry-picked onto the release branch and shipped as a new tag.1 cut branch2 tag anddeploy3 cherry-pickmainwork continues hererelease/1.0only fixes go hereTagged releasev1.0.0, v1.0.1Fix on mainone commit
  • Data
  • Control
  • Runs code

A release branch is cut from main and deployed as a tagged version; a fix made on main is cherry-picked onto the release branch and shipped as a new tag.

  1. main to release/1.0: 1 cut branch
  2. release/1.0 to Tagged release: 2 tag and deploy
  3. Fix on main to release/1.0: 3 cherry-pick

Trunk-based development keeps branches short, often a day or two, and merges them into main many times a week. Conflicts stay small because everyone sees everyone's work quickly. The price is discipline: main must stay releasable, so you need dependable automated tests, and unfinished features must be hidden behind a feature flag, a setting that turns code on or off without a new deploy.

Long-lived feature branches give each feature its own space and keep main calm until the work is done. The price is a large, late merge. Two long branches can each pass their tests and still break when joined. This layout can work for small teams with infrequent releases, and it gets harder as the team or the pace grows.

Release branches suit software where several versions are in use at once, or where a release needs a stabilisation period. You cut release/1.0 from main, tag what you ship, and apply fixes to it with git cherry-pick, which copies one commit onto another branch. The price is that every fix must be applied in more than one place and you must remember to do it.

Try it yourself

Try the release-branch pattern in a throwaway repository.

  1. 1
    Create a project and cut a release
    mkdir git-lab-6 && cd git-lab-6
    git init -b main
    git config user.name "Your Name"
    git config user.email "you@example.com"
    echo "hello wrold" > app.txt
    git add app.txt && git commit -m "Add app"
    git switch -c release/1.0
    git tag v1.0.0
    git switch main

    The tag marks exactly what you would have shipped as 1.0.0. Tags do not move.

  2. 2
    Fix a bug on main
    echo "hello world" > app.txt
    git commit -am "Fix typo in greeting"
  3. 3
    Copy the fix onto the release branch
    git switch release/1.0
    git cherry-pick main
    git tag v1.0.1
    git log --oneline --graph --all --decorate

    git cherry-pick main copies the latest commit on main onto the current branch. The copy has a different commit ID because its parent is different. The graph shows two separate commits with the same message.

  4. 4
    Ask which tag contains a commit
    git describe --tags
    git tag --contains HEAD

    Questions like what version is this are why tagging what you deploy pays off.

What breaks, and where to look

  • A pull request shows hundreds of changed files, and merging it produces conflicts in many places.

    WHY IT HAPPENS
    The branch lived for a long time while main kept moving, so the two lines have drifted far apart.
    CHECK FIRST
    Run git log --oneline main..feature to count what you added, and git diff --stat main...feature for the size of the change. Merge or rebase main into the branch regularly, and split the work into smaller pull requests.
  • The same fix appears twice with different IDs, and merging the release branch back into main produces conflicts.

    WHY IT HAPPENS
    A cherry-pick copies a change into a new commit instead of linking it, so git sees two unrelated commits that touch the same lines.
    CHECK FIRST
    Run git log --oneline --left-right --cherry-pick main...release/1.0 to list only commits that are not equivalent on the other side. Decide in advance whether fixes flow main first, then to the release branch, and keep it consistent.
  • Production is running something, and nobody can say which commit or branch it came from.

    WHY IT HAPPENS
    Deploys were made from a branch that kept moving, with no tag or recorded commit ID.
    CHECK FIRST
    Record the commit ID the pipeline built. Locally use git branch --contains <id> and git tag --contains <id>, and git describe --tags <id> to name it. Tag every release.

When not to use this

  • If you work alone on a small project, committing straight to main with a few short branches is fine. Adding a release process nobody needs is overhead.
  • Trunk-based development without automated tests and feature flags is risky, because every merge can break what ships. Build the safety net first.
  • Release branches add real bookkeeping. If you deploy one version of a service continuously and can roll back quickly, you probably do not need them.

Check yourself

Why do long-lived branches hurt?

Other work lands on main in the meantime, so the final merge is large and conflicts pile up.

What does a feature flag let you do?

Merge and even deploy unfinished code that stays switched off, so branches can stay short.

Why does a cherry-picked commit get a new ID?

A commit's ID depends on its parent. The copy sits on a different parent, so it is a different commit.

Lesson 37 of 92 in the DevOps path

Search

Search cheat sheets and projects