Conventional Commits
Release automation needs to know, for every commit, "is this a feature, a fix, or a breaking change?". Conventional Commits 1.0.0 is a small specification for writing that into the commit message so a machine can read it. git-cliff, semantic-release, release-please, commitlint and many others all speak it.
The format
<type>[optional scope][!]: <description>
[optional body]
[optional footer(s)]feat(parser)!: accept YAML 1.2 only
YAML 1.1 booleans such as "yes" and "on" are now strings.
BREAKING CHANGE: configs relying on yes/no booleans must use true/false.
Refs: #412| Part | Rules |
|---|---|
| type | a noun; feat (new feature → MINOR) and fix (bug fix → PATCH) are defined by the spec; others are allowed (the widespread Angular set: build, chore, ci, docs, perf, refactor, revert, style, test) and don't trigger a release unless you configure them to |
| scope | optional, a noun in parentheses describing the area: fix(parser): |
! |
right before the colon: a breaking change (→ MAJOR) |
| description | right after : ; a short summary |
| body | free text after one blank line |
| footers | after one blank line, Token: value or Token #value (git trailer style); tokens use - instead of spaces, except BREAKING CHANGE |
BREAKING CHANGE: |
footer (uppercase; BREAKING-CHANGE is a synonym) that marks a breaking change and describes it |
A parser that follows the spec
Writing the parser shows how little is needed, and what tools do with each part. This one handles the header, !, the body, footers and the breaking-change footer, then derives the release type:
import re
HEADER = re.compile(r"^(?P<type>[A-Za-z]+)(?:\((?P<scope>[^()\r\n]+)\))?(?P<bang>!)?: (?P<desc>.+)quot;)
FOOTER = re.compile(r"^(?P<token>BREAKING[ -]CHANGE|[A-Za-z][\w-]*)(?::\s|\s#)(?P<value>.*)quot;)
def parse(message):
lines = message.strip("\n").split("\n")
m = HEADER.match(lines[0])
if not m:
return None
rest = "\n".join(lines[1:]).strip("\n")
paragraphs = rest.split("\n\n") if rest else []
footers = {}
# the last paragraph is the footer block if its first line looks like a footer
if paragraphs and FOOTER.match(paragraphs[-1].split("\n")[0]):
for line in paragraphs.pop().split("\n"):
f = FOOTER.match(line)
if f:
footers[f["token"]] = f["value"]
breaking = bool(m["bang"]) or any(t in footers for t in ("BREAKING CHANGE", "BREAKING-CHANGE"))
return {"type": m["type"].lower(), "scope": m["scope"], "desc": m["desc"],
"body": "\n\n".join(paragraphs), "footers": footers, "breaking": breaking}
def release_type(commit):
if commit is None:
return "invalid"
if commit["breaking"]:
return "major"
return {"feat": "minor", "fix": "patch", "perf": "patch"}.get(commit["type"], "none")
messages = [
"feat(parser)!: accept YAML 1.2 only\n\nYAML 1.1 booleans are now strings.\n\n"
"BREAKING CHANGE: configs relying on yes/no must use true/false.\nRefs: #412",
"fix: handle empty input\n\nReviewed-by: Grace\nRefs: #7",
"feat(cli): add --json",
"docs: explain the plugin API",
"refactor(core): split the cache module\n\nBREAKING CHANGE: Cache is no longer exported from the root package.",
"Fix bug",
"feat : extra space before the colon",
]
for msg in messages:
c = parse(msg)
head = msg.split("\n")[0]
print(f"{release_type(c):<8} {head}")
if c and c["footers"]:
print(f" footers: {c['footers']}")
bumps = [release_type(parse(m)) for m in messages]
order = ["none", "patch", "minor", "major"]
print("release for the whole batch:", max((b for b in bumps if b in order), key=order.index))major feat(parser)!: accept YAML 1.2 only
footers: {'BREAKING CHANGE': 'configs relying on yes/no must use true/false.', 'Refs': '#412'}
patch fix: handle empty input
footers: {'Reviewed-by': 'Grace', 'Refs': '#7'}
minor feat(cli): add --json
none docs: explain the plugin API
major refactor(core): split the cache module
footers: {'BREAKING CHANGE': 'Cache is no longer exported from the root package.'}
invalid Fix bug
invalid feat : extra space before the colon
release for the whole batch: major
Note the fifth message: a refactor would normally not trigger a release at all, but its BREAKING CHANGE: footer makes it a major one. Any type can be breaking.
Mapping commit types to releases
| Commit | semantic-release (default) | git-cliff --bump (default) |
Changelog section (typical) |
|---|---|---|---|
fix: |
patch | patch | Bug Fixes |
feat: |
minor | minor | Features |
perf: |
patch | patch | Performance |
feat!: or BREAKING CHANGE: footer |
major | major, even from 0.x (breaking_always_bump_major) |
⚠ Breaking Changes |
revert: |
patch | patch | Reverts |
docs:, chore:, ci:, test:, refactor:, style:, build: |
no release | patch (any parsed commit bumps at least the patch) | usually hidden |
Defaults differ between tools, and you will see the real behaviour of each in its own lesson. Decide your mapping once and configure every tool the same way.
Making it work in a team
- Squash merges + PR titles. If PRs are squash-merged, the PR title becomes the commit message on
main. Lint the PR title instead of every WIP commit on the branch (commitlint shows both). - Merge commits.
Merge pull request #12 from …is not conventional; tools skip merge commits by default, but then the merged commits must be conventional. - The description is for humans. It becomes a changelog line. "fix: handle empty input" helps a user; "fix: stuff" or "fix: address review comments" doesn't.
- Scopes from a fixed list. Free-form scopes drift (
ui,UI,frontend); commitlint'sscope-enumrule enforces a list. - Breaking changes need a footer.
!alone marks the commit; theBREAKING CHANGE:footer tells users what to do, and changelog generators print it.
Test yourself
Which commit triggers a MAJOR release under Conventional Commits?
feat: add a big new featurerefactor: rename internalswith aBREAKING CHANGE:footerfix!without a colonchore: bump major version
Is feat : add X (space before the colon) valid?
- Yes, whitespace is ignored
- No: the type must be followed directly by
(scope),!or: - Only for feat
- Only in the body
With squash merges, which text should commitlint check?
- Every commit on the feature branch
- The pull request title, which becomes the commit on main
- Only the PR description
- Nothing