Examples of using conventional commits and standard-version with Git flow.
The Git flow branches that we are interested in are the following branches :
develop
(long lived) - latest development work, deploys to a dev environmentrelease/*
(short lived) - release candidate, bug fixes for a release, deploys to a test environmentmaster
(long lived) - last release, deploys to a production environmenthotfix/*
(short lived) - urgent fixes to production
standard-version
uses the format of your commit messages to determine the next semver version number of your release in the format major.minor.patch.
If you have only applied fix: ...
commits, then it will bump your patch number, if you have applied a feat: ...
commit, then it will bump your minor version, and if you have applied a BREAKING CHANGE
commit, then it will bump your major number. See conventional commits for more details.
This means that if your starting version number is 1.0.0
and you apply ten fix: ...
commits, then your next version number will be 1.0.1
, not 1.0.10
. Likewise, if you applied ten fix: ...
commits and ten feat: ...
commits, then your next version number will be 1.1.0
.
- Run
git flow init
and configure all of the default options - When using
standard-version
for versioning, you need to add apackage.json
file to the root of your Git repository - You can install yarn and use
yarn init
- Add
standard-version
as a development dependency withyarn add standard-version -D
- Add a
release
script in your package.json:
"scripts": {
"release": "standard-version"
},
"standard-version": {
"skip": {
"tag": true
}
}
Note as we will be leaving release tags to
git flow
, we disable them instandard-version
.standard-version
will take care of bumping ourpackage.json
file with the version number, and updating theCHANGELOG.md
file with changes in each release.
- Create a branch off of
develop
- If you wish to maintain pull requests so that changes are reviewed and accepted to
develop
, then you can choose not use thegit flow
commands for feature branches, and instead just push yourfeature/...
orbugfix/...
branches to a remote equivalent and create a pull request todevelop
- Branches can actually be called anything except
develop
,master
,release/*
, orhotfix/*
- Commit messages should follow conventional commits, e.g.
feat: ...
for features, andfix: ...
for fixes - Other work which shouldn't affect the version number should also follow a standard commit message structure, e.g.
docs: ...
orrefactor: ...
- Do not choose a version number yourself for your
release/*
branch, instead get the next version number based from your conventional commits. - See an example script that you can add to your
package.json
. - With the version number calculated for you, use the
git flow release start <version>
to start a release - This will create a
release/<version>
branch - Within this branch, you should then run
yarn run release
to increment the version number withinpackage.json
automatically to match the release branch name, as well as updating theCHANGELOG.md
file automatically
- Your
release/<version>
branch should deploy to a test environment - Based on testing feedback, you may need to fix a bug for that release, whilst the
develop
branch has continued on into development for the next release - To do this you can create a branch off of your
release/<version>
branch - If you wish to maintain pull requests for release bug fixes, then you can push your e.g.
bugfix/...
branch to a remote equivalent and create a pull request intorelease/<version>
- You should not run
standard-version
(yarn run release
) after merging a release bugfix, as you want the release version to stay the same (the release branch name and version should be immutable) - Any release bug fixes will be included in the version calculation for your next release, as well as being included in the next
CHANGELOG.md
- Once happy, you can merge your
release/<version>
branch intomaster
and any changes back intodevelop
withgit flow release finish <version>
- This also creates a tag with the version number (TODO: is this an issue with standard-version and git flow both creating a release tag?)
- The release branch is also automatically deleted
- Your production build should then begin from the updated
master
branch and your latest release is now ready to deploy frommaster
- If you need to fix a critical bug in production, then you need to create a hotfix
- These are branches off of
master
and can be created withgit flow hotfix start <version>
- as a hotfix is a fix, you can just increment the patch version number from the last completed release, e.g. ifmaster
is release 1.0.1, then create ahotfix/1.0.2
branch - You must then update the
package.json
file to in the hotfix branch to match the hotfix branch version number (otherwise thedevelop
branch will not have its version updated when you finish the hotfix, and the tagging will fail on the next release start) - You should not run
standard-version
as you do not wish to update theCHANGELOG.md
- the hotfix will be included in the next releaseCHANGELOG.md
- Once you have made the hotfix, you should then merge it into
master
and back into thedevelop
branch usinggit flow hotfix finish <version>
- This does mean you lose the ability to do pull requests on hotfixes, and it also means you need push permission to
master
to be possible. TODO: are there ways around this? - You should not run
standard-version
when your hotfix is merged into master, as the fix will be included in the next release version calculation and in the nextCHANGELOG.md