Sub topic 14
Release and hotfix branches
This chapter assumes you can create branches, merge with --no-ff, and tag commits. It also assumes you know what develop and main mean in Gitflow. Here you will see how release and hotfix branches fit into that model.
Why Gitflow adds two more branch types
In Gitflow, develop collects finished features and main holds only released code. Release and hotfix branches move code between the two without disturbing either one's work.
- A release branch stabilizes a set of features that are about to ship.
- A hotfix branch fixes a bug in code that is already in production.
Release branches
You cut a release branch from develop when the planned features are done. Name it after the version, such as release/1.4.
On this branch you only fix bugs, update version numbers, and write release notes. New features go to develop and wait for the next release. This lets the team keep working while the release is tested.
When testing passes, you merge the release branch into main, tag the commit, and merge it back into develop. The merge back matters because bug fixes made on the release branch must not be lost.
Hotfix branches
A hotfix handles an urgent production bug. You branch from main, not develop, because develop contains unreleased work that you do not want to ship.
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 sequence has four steps:
- Branch from
mainand commit the fix. - Merge into
mainwith--no-ff, so the merge commit records the hotfix. - Tag the result with an annotated tag, which is what you deploy.
- Merge into
developtoo, then delete the branch.
Common errors
- Forgetting the merge back into
develop. The bug returns in the next release, becausedevelopnever received the fix. - Branching a hotfix from
develop. This ships unfinished features to production. - Adding features to a release branch. This delays the release and defeats its purpose.
- Skipping the tag. Without a tag, you cannot easily find what was deployed.
- Leaving branches open. A long-lived release branch drifts from
developand makes later merges harder.
Exercise
Production is running v1.4.0, and develop already contains half-finished work for 1.5. A critical bug is reported. Which branch do you start the fix from, and which branches must receive it?
Show answer
Start from main, for example hotfix/1.4.1. Merge the fix into main and tag it v1.4.1. Then merge it into develop so the fix is not lost. If a release branch is open, merge it there instead of develop.
Exercise
Why do you merge a release branch back into develop after merging it into main?
Show answer
Bug fixes made on the release branch exist only there. Merging back brings them into develop, so the next release does not reintroduce the bugs.