Sub topic 8
How commits map to MAJOR, MINOR, and PATCH
This chapter assumes you know Semantic Versioning (MAJOR.MINOR.PATCH) and can read a Conventional Commits message: type, optional scope, description, body, and footers. Here you will see how each commit in a history implies a version bump, and how a tool can compute the next version from it.
The mapping
Semantic Versioning ties the version number to the kind of change. Conventional Commits was designed to line up with it, so the commit type tells you which number to raise.
| Commit | SemVer effect | Example |
|---|---|---|
| `fix:` | PATCH (1.4.2 to 1.4.3) | `fix: prevent crash when the config file is empty` |
| `feat:` | MINOR (1.4.2 to 1.5.0) | `feat(parser): add support for array literals` |
| Any type with `!` or a `BREAKING CHANGE` footer | MAJOR (1.4.2 to 2.0.0) | `feat(api)!: remove the deprecated /v1/users endpoint` |
Other types, such as docs, style, refactor, test, build, ci, and chore, have no SemVer effect by default. A release made only of those commits usually needs no new version. Some teams choose to treat perf or refactor as PATCH, but that is a tool setting, not part of the specification.
A breaking change can be attached to any type. A fix!: is a MAJOR bump, because the marker overrides the type.
Choosing the highest bump
A release often contains many commits. The next version comes from the highest-level change among them, and only one number is raised.
fix: handle empty config file
fix(parser): trim trailing whitespace
feat(cli): add --verbose flag
docs: update install instructionsThe two fixes alone would give a PATCH. The feat raises that to MINOR. The docs commit is ignored. From 1.4.2 the next version is 1.5.0. If one more commit carried a BREAKING CHANGE footer, the result would be 2.0.0.
Raising a higher number resets the lower ones to zero. A MINOR bump sets PATCH to 0, and a MAJOR bump sets both MINOR and PATCH to 0.
Computing it yourself
Release tools do this by reading the log. You can imitate the idea with plain Git. This script finds the highest bump since the latest tag. It assumes at least one tag exists.
#!/bin/sh
last=$(git describe --tags --abbrev=0)
log=$(git log "$last"..HEAD --format='%s%n%b')
if echo "$log" | grep -Eq '^BREAKING[ -]CHANGE:|^[a-z]+(\([^)]*\))?!:'; then
echo "major"
elif echo "$log" | grep -Eq '^feat(\([^)]*\))?:'; then
echo "minor"
elif echo "$log" | grep -Eq '^fix(\([^)]*\))?:'; then
echo "patch"
else
echo "none"
fiThe script checks subjects and bodies, so it sees both the ! marker and the footer. Real tools parse messages more carefully, but the decision order is the same.
Common errors
- Using
featfor a change that breaks users but forgetting the!. The tool then proposes a MINOR release and users get a surprise. - Using
fixfor a new capability. The release is only a PATCH, and the new feature is hidden in the changelog. - Marking an internal refactor as
feat. This inflates the version for no user-visible reason. - Before 1.0.0, SemVer treats the API as unstable. Many tools offer a setting that turns breaking changes into MINOR bumps while the major version is 0.
Exercise
The last release is 2.3.4. Since then the history contains fix: correct date parsing, docs: fix typo in README, and refactor(core)!: rename the init option to setup. What is the next version, and why?
Show answer
3.0.0. The ! on the refactor commit marks a breaking change, which forces a MAJOR bump regardless of its type. The MAJOR bump resets MINOR and PATCH to 0. The fix would only have given a PATCH, and the docs commit has no effect.
Exercise
The last release is 0.9.1 and the history since then holds two feat commits and one fix commit, with no breaking changes. What version does a standard SemVer-based tool propose?
Show answer
0.10.0. The highest change is feat, so MINOR rises once, not twice, and PATCH resets to 0.