Sub topic 13
Gitflow
Gitflow is a branching model with two permanent branches and three kinds of temporary branches. It suits teams that ship versioned releases on a schedule. This chapter assumes you can merge, tag, and push, and that you know what a long-lived branch is.
The branches
mainholds only released code. Every commit on it is a release, and each one is tagged.developis the integration branch. Finished features land here.feature/*branches start fromdevelopand merge back intodevelop.release/*branches start fromdevelopwhen a release is ready to stabilize. Only bug fixes and version bumps go on them.hotfix/*branches start frommainto fix a production bug, without waiting for the next release.
The release flow
When develop has the features you want, create release/1.5 from it. QA and bug fixing happen on that branch while new feature work continues on develop. When the release is ready, merge it into main and tag it. Then merge it into develop as well, so the fixes made during stabilization are not lost.
Gitflow normally uses --no-ff merges. The merge commit records that a branch existed, so you can see where a feature or release began and ended.
A hotfix
A production bug cannot wait for the release cycle, so the fix starts from main.
git switch -c hotfix/1.4.1 main
# fix the bug and commit
git switch main
git merge --no-ff hotfix/1.4.1
git tag -a v1.4.1 -m "Hotfix 1.4.1"
git switch develop
git merge --no-ff hotfix/1.4.1
git branch -d hotfix/1.4.1The fix is merged into two branches. main gets it so production is repaired, and develop gets it so the next release does not reintroduce the bug. If a release/* branch is open at the time, merge the hotfix there too.
Common errors
- Forgetting the merge back to
develop. The bug returns in the next release. Treat "merge tomain" and "merge todevelop" as one step. - Adding features to a release branch. Release branches accept fixes only. New work goes to
developand waits for the next release. - Letting feature branches live too long. Long branches drift from
developand produce painful conflicts. Mergedevelopinto them regularly, or rebase them if you are the only person using the branch. - Untagged releases. Without tags on
main, you cannot easily find what was shipped.
Costs and fit
Gitflow gives clear separation between released and unreleased code, and a place for stabilization. The price is more branches, more merges, and a delay between finishing code and shipping it. Teams that deploy several times a day usually find this overhead hard to justify, and often prefer GitHub Flow or trunk-based development. Gitflow fits best when you support several released versions, ship on a fixed schedule, or cannot deploy continuously, such as with installed software or mobile apps.
Exercise
A critical bug is found in version 1.4.0 in production while release/1.5 is open. Which branch do you start the fix from, and into which branches do you merge it?
Show answer
Start hotfix/1.4.1 from main. Merge it into main and tag it. Also merge it into develop, and into release/1.5 because that release is open and would otherwise lack the fix.