A successful Git branching model
Main Branches
- master
- develop
Supporting brach
- Release
- Hotfix
- Feature
Feature Branches
Feature branches are used to develop a new feature for an upcoming or distant future release.
Feature branches only exist in developers repos only i.e. in their machine, not in origin i.e. remote.
- branch from develop
- must be merged back in develop
- Naming convention -> Anything except
- master
- develop
- release-*
- hotfix-*
Creating a feature branch
$ git checkout -b myfeature develop
After finishing the feature and merge back to develop
$ git checkout develop
Switch to develop
$ git merge --no-ff myfeature
Plain merge with out fast-forward
$ git merge myfeature
Merge without fast-forward
$ git branch -d myfeature
Delete your feature branch
$ git push origin develop
Push changes to develop branch
It is impossible to see from git history of which commit objects together implement a feature. You would have to manually read all commit messages. If you need to revert particular feature, it is true headache in later situations, whereas it is easy with --no-ff flag was used.
It will create more empty commits but the gain is much bigger than cost.
Release Branches
- Branch from develop
- Must merge back in develop and master
- Branch naming conventions
- release-*
Creating release branch
$ git checkout -b release-1.2.1 develop
Switch to new branch
$ ./bump-version.sh 1.2.1
Files modified successfully and bump to 1.2.1
$ git commit -a -m "Bump version number 1.2.1"
[release-1.2.1 a4d9318] Bumped version number to 1.2.1
Finishing release branch
$ git checkout master
Switch to master branch
$ git merge --no-ff release-1.2.1
Merge made by recursive
$ git tag -a 1.2.1
or
$ git tag -a 1.2.1 -m "Release version 1.2.1"
This is annotated tags
To keep changes made by release branch, we need to merge in develop
$ git checkout develop
Switch to develop branch
$ git merge --no-ff release-1.2.1
Merge made recursive.
This may lead to merge conflict. If so, fix and commit it.
Finally
$ git branch -d release-1.2.1
Deleted branch was af23b1
Hotfix Branches
- Branch from master
- Must merge back in develop and master
- Branch naming conventions
- hotfix-*
Hotfix branches are very much like release branches in that they are meant to prepare for production, albeit unplanned. When a critical bug in production version must be resolved immediately, a hotfix branch may be branch of from corresponding tag on master branch that marks production version.
Creating hotfix branch
$ git checkout -b hotfix-2.0.3 master
Switch to new hotfix branch hotfix-2.0.3
$ ./bump-version.sh 2.0.3
Files modified successfully
$ git commit -a -m "Bump to version 2.0.3"
[hotfix-2.0.3 af21d3] bump version to 2.0.3
1 files are changed 1 insertions(+), 1 deletions(-)
Don't forget to bump a version after branching off.
Then fix the bug and commit changes
$ git commit -m "Fix severe production problem"
Severe bug problem solved
5 files are changed 10 insertions(+), 3 deletions(-)
Finishing hotfix branch
When finished the bug fixed, we need to merge back in master as well as develop branch, in order to safeguard that the bug fix also include in next release as well.
$ git checkout master
Switched to branch master
$ git merge --no-ff hotfix-2.0.3
Merge made by recurssive
$ git tag -a 2.0.3
Add simple tag or you can annote it like
$ git tag -a 2.0.3 -m "Severe bug fix"
Next, include the bugfix in develop also.
$ git checkout develop
Switched to branch master
$ git merge --no-ff hotfix-2.0.3
Merge made by recurssive
The one exception rule is that, When a release branch currently exists, the hotfix branch need to be merge in release branch instead of develop. Back merging the bug fix into release branch eventually result in the hotfix branch merge in develop too, when release branch is finished. (If work in develop immediately require the hotfix and cannot wait it fo release branch to finish, you can safely merge in develop branch now already as well.)
Finally, remove the temporary branch
$ git branch -d hotfix-2.0.3
Deleted branch hotfix-2.0.3
You can find full documentation and explanation here. Feel free to check it out.
Other links to learn and embrace the power of Git.