Improve your Git Commit workflow
###
Warning: This blog post is outdated. Please visit the standard-commit repository.
###
The following steps lead you to improve your git commit messages as well as automate changelog and versioning.
Goals
- Enforce a standard on every commit respecting Conventional Commits specification
- Automatically bumps the version in package.json respecting Semantic Versioning
- Automatically updates CHANGELOG.md
- Tags a new release
Installation
- husky will trigger the commitlint on each commit
- commitlint checks if your commit messages meet the Conventional Commits format
- commitizen and cz-conventional-changelog helps format commit messages with a series of prompts
- standard-version will update CHANGELOG.md, bump the version and generate a new tag
Step 1 — Install husky, commitlint, cz-conventional-changelog, and standard-version locally
yarn add --dev husky @commitlint/cli @commitlint/config-conventional cz-conventional-changelog standard-version
Step 2 — Install commitizen globally
sudo yarn global add commitizen
Step 3 — Update package.json
{
..."scripts": {
"release": "standard-version"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog",
"disableScopeLowerCase": true
}
}
}
Step 4 — Create a commitlint.config.js file in your project root directory
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-case': [0],
},
};
Step 5 — Configure commitizen
commitizen init cz-conventional-changelog --save-dev --save-exact
Usage
Basically, instead of typing git commit
now you type git cz
which will open a wizard and help you write a standardized message.
Commit with commitizen
Using commitizen to prompts a wizard
git add .
git cz
yarn release
git push --follow-tags
Pro tip
Run this command in your first release to prevent bumping the version in package.json
yarn release --first-release
Normal commit command
You can still use git commit ...
but the commit will fail if the commit message is not properly formatted.
git add .
git commit -m "feat(blog): add comment section"
yarn release
git push --follow-tags
Commit but skip changelog, bump version, and tag
git add .
git cz
yarn release --skip.changelog --skip.bump --skip.tag
git push
Credits
- Git hooks — husky
- Lint commit messages — commitlint
- Commit messages — commitizen
- Automate versioning — standard-version
Conclusion
With these four packages, you can easily write meaningful commit messages with Conventional Commits. Besides, you will get the changelog automatically generated and with the correct version numbers. All this without any effort. This is awesome.
This article helped you? Please consider giving a few claps 👏.