Lesson 7 of 8 in Git and collaboration
Branching strategies and trade-offs
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
- 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.
- Short branch to PR and checks: 1 open PR
- PR and checks to main: 2 merge
- main to Pipeline: 3 ship
- Pipeline to Feature flag: ships behind it
- 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.
- main to release/1.0: 1 cut branch
- release/1.0 to Tagged release: 2 tag and deploy
- 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.
- 1Create 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.
- 2Fix a bug on main
echo "hello world" > app.txt git commit -am "Fix typo in greeting"
- 3Copy 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 maincopies the latest commit onmainonto 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. - 4Ask 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..featureto count what you added, andgit diff --stat main...featurefor 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.0to 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>andgit tag --contains <id>, andgit 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
mainwith 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.
Keep going
Lesson 37 of 92 in the DevOps path