Git Branching Strategies
Level 5 · Working as a team
How teams decide which branches exist and how work gets back to main. Trunk-based development against Git Flow, and where a monorepo fits in.
Course contentsLesson 13 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Say what a branching strategy decides
- Compare trunk-based development and Git Flow
- Pick a strategy for your team
13 mincovers 5 glossary entries
Worth reading first: Branch Protection and Hooks
Trunk-based development or Git Flow
A branching strategy is a team's rules for branches: which ones exist, how long they live, and how work gets back to main. The two best known are trunk-based development and Git Flow. Trunk-based teams merge into main at least once every 24 hours. Git Flow keeps two long-lived branches and three short kinds.
In trunk-based development, everyone works on one main branch, called the trunk. Branches, if any, are tiny and last a day or less. Small changes join often, so conflicts stay small. Tests run on every merge, because a broken trunk breaks everyone at once.
Half-done work still lands on the trunk, hidden behind a feature flag. A feature flag is a switch in the code that keeps a new feature off for users until it is ready. The code ships every day. The feature appears when someone turns the switch on.
Git Flow was set out in a blog post in 2010. Main holds what is live, and develop holds the next release. Feature branches come off develop and merge back into it. A release branch carries develop into main. A hotfix branch fixes main fast, and is merged into both.
Git Flow suits software shipped as numbered versions, such as an app where version 3 and version 4 are both still in use. In 2020 its author added a note to the post. Teams that ship a website many times a day, it says, should use something simpler.
A monorepo is a related choice: one repository that holds many projects. A web app, a phone app and their shared code can sit in one repo, in separate folders. One commit can then change all three at once. The cost is size, so big monorepos need tools that build and test only what changed.
The common mistake is copying a heavy strategy a small team does not need. Five kinds of branch for a team of three means more merging than coding. Start with main plus short feature branches, and add rules only when a real problem shows up.
Two branching strategies side by side
| Question | Trunk-based development | Git Flow |
|---|---|---|
| Long-lived branches | One: main | Two: main and develop |
| How often work joins | At least once a day | When a feature is done |
| Half-done work | Hidden behind feature flags | Kept on feature branches |
| Suits | Websites shipped often | Apps with numbered versions |
Which one to pick
- 1
When you ship a website or service many times a week, and your tests are good.
- 2
When you ship numbered versions, and must keep fixing old ones people still use.
Worked example
A team of 4 drops Git Flow
Jack's team of 4 runs a shopping website and uses Git Flow. Each finished feature waits about 2 weeks for the next release branch.
They switch to trunk-based development with feature flags. Changes now reach the site the same day, and the long merge on release day is gone.
The words for this lesson5
Check yourself
How do trunk-based teams ship code for a feature that is not finished?
Finished this lesson?
Marking it complete also marks its 5 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.