Sub topic 2
The structure: type, scope, description, body, footers
This chapter assumes you can stage and commit changes, read git log, and use git commit -m. It also assumes you know that Semantic Versioning uses MAJOR.MINOR.PATCH. Later chapters cover each part in detail. Here you learn how the parts fit together.
The overall shape
A conventional commit message has a fixed layout. Only the first line is required in practice. The body and footers appear only when they add something.
<type>[optional scope][!]: <description>
[optional body]
[optional footer(s)]The parts are separated by punctuation and blank lines. A parser relies on these separators, so they matter as much as the words.
| Part | Required | Separator | Purpose |
|---|---|---|---|
| type | yes | followed by `:` and a space | Names the kind of change, such as `feat` or `fix` |
| scope | no | parentheses right after the type | Names the area of the code that changed |
| `!` | no | right before the colon | Marks a breaking change |
| description | yes | after `: ` | One-line summary of the change |
| body | no | one blank line before it | Explains the motivation and the details |
| footers | no | one blank line before them | Holds metadata in `token: value` form |
The smallest valid message
The minimum is a type, a colon, a space, and a description.
fix: prevent crash when the config file is emptyEverything else is optional. A missing space after the colon makes the message invalid, so fix:prevent crash fails.
A full message
This example uses every part except the breaking-change marker.
feat(parser): add support for array literals
The parser now accepts [1, 2, 3] syntax and produces an ArrayNode.
Refs: #482Reading it from the top:
featis the type. It says this commit adds a feature.(parser)is the scope. It says the change is in the parser.add support for array literalsis the description. It finishes the sentence "This commit will...".- The paragraph after the first blank line is the body. It adds detail the description has no room for.
Refs: #482is a footer. It links the commit to an issue.
How the parts are separated
Three rules cover most layout errors.
- The first line holds the type, scope,
!, colon, space, and description. Keep it on one line. - A blank line separates the first line from the body. A blank line also separates the body from the footers.
- The body is free-form text. It can have several paragraphs.
Footers follow a git trailer style. Each footer is a word token, then : or #, then a value. Tokens use hyphens in place of spaces, such as Reviewed-by. The one exception is BREAKING CHANGE, which may contain a space.
fix(auth): reject expired refresh tokens
Expired tokens were accepted for up to a minute because the check
ran before the clock was synchronized.
Reviewed-by: Dana
Refs: #517Here the body is a single paragraph. There are two footers, one per line.
Case and capitalization
The specification treats the type and scope as case-insensitive in comparisons, with one exception. BREAKING CHANGE must be uppercase. Most teams write types in lowercase anyway, so history stays consistent and easy to search.
Common structure errors
- Missing space after the colon:
feat:add login. - A trailing period or capital letter habits copied from prose are style choices, not structure errors, but pick one rule and keep it.
- Putting the scope after the colon:
feat: (parser) add arrays. The scope must come before the colon. - Writing a footer directly under the description with no blank line.
Check your understanding
Exercise
A teammate writes this message. Name the two structural problems and show the corrected version. feat(search): add fuzzy matching Allows typos of one character. Refs #90
Show answer
There is no blank line between the description and the body, and none between the body and the footer. Also Refs #90 is missing its separator, so it should be Refs: #90 or Refs #90 only if your tools accept the # form (the specification allows token: value or token #value, so Refs #90 is acceptable but Refs: #90 is clearer). Corrected:
feat(search): add fuzzy matching
Allows typos of one character.
Refs: #90
Exercise
Which of these is a valid conventional commit first line: fix(api) handle null id, fix(api): handle null id, or fix: (api) handle null id?
Show answer
Only fix(api): handle null id. The first lacks the colon. The third puts the scope after the colon, where it is read as part of the description.