Sub topic 6
Using the body and footers
This chapter assumes you can write a basic conventional commit header (type, optional scope, description) and know how git commit opens an editor for longer messages. Here you add the two optional parts that follow the header: the body and the footers.
Where the body and footers sit
A conventional commit is laid out in this order:
- The header:
type(scope): description - A blank line
- The body (optional)
- A blank line
- One or more footers (optional)
The blank lines are not decoration. Tools use them to tell where one part ends and the next begins. If you leave one out, a parser may read your body as part of the description or your footer as part of the body.
The body
The body explains the change. The description says what changed. The body says why, and anything a reviewer needs in order to understand the choice you made. The specification puts almost no limits on it. It is free-form text and can run to several paragraphs, separated by blank lines.
feat(parser): add support for array literals
The parser now accepts [1, 2, 3] syntax and produces an ArrayNode.
Nested arrays are supported. Trailing commas are not, because the
grammar treats them as a syntax error elsewhere.
Refs: #482Use the body when the diff alone would leave a future reader asking "why?". Typical cases are a non-obvious design choice, a rejected alternative, a workaround for a known bug, or a behavior change that is hard to see in the code. A small, self-explanatory change such as a typo fix needs no body.
A practical habit is to wrap body lines at about 72 characters. The specification does not require this, but it keeps messages readable in git log and in terminals.
Footers
A footer is a line of metadata in the form token: value. The token comes first. It is followed by either a colon and a space, or a space and a hash sign (#). Footer tokens use hyphens in place of spaces, such as Reviewed-by, so that they are easy to tell apart from body text. BREAKING CHANGE is the one exception and keeps its space.
fix(auth): reject expired refresh tokens
Expired tokens were accepted when the clock skew was under a minute.
Reviewed-by: Dana Okafor
Refs: #1031, #1044Common footers include:
| Footer token | Typical use |
|---|---|
| `Refs` | Link to related issues or commits |
| `Closes` / `Fixes` | Issue-tracker keywords that close an issue when the commit lands, on hosts that support them |
| `Reviewed-by` | Credit for a reviewer |
| `Co-authored-by` | Credit for another author |
| `BREAKING CHANGE` | Describes an incompatible change (next chapter) |
The specification does not fix a list of tokens, so teams pick their own. Whatever you choose, use it the same way every time. Tools that read footers match the token text exactly, so Refs and refs may not be treated alike.
Writing a message with a body from the command line
Passing -m more than once creates separate paragraphs, with a blank line between each. This is a quick way to write a header, body, and footer without opening an editor.
git commit \
-m "fix(cache): evict entries when the size limit is reached" \
-m "The cache grew without bound under sustained writes. Entries are now evicted oldest-first once the configured limit is hit." \
-m "Closes: #212"Check the result with git log -1. If the footer is missing its blank line, or the body has swallowed it, amend the commit with git commit --amend.
Common mistakes
- Missing blank line before the footer. The footer is then parsed as part of the body, and tools that look for it will not see it.
- Using spaces in footer tokens. Write
Reviewed-by, notReviewed by. OnlyBREAKING CHANGEis exempt. - Repeating the diff in the body. Explain intent and consequences. The code already shows the mechanics.
- Putting the reason in the description. If the header grows past one short line, move the extra detail into the body.
Exercise
A commit has the header "fix(db): retry on connection reset". You want to add a paragraph explaining that resets happen during failover, and link issue 318. Write the full message, and say which blank lines are required.
Show answer
fix(db): retry on connection reset Connection resets occur while the primary node fails over. Retrying once after a short delay lets the request succeed on the new primary. Refs: #318 A blank line is required between the header and the body, and another between the body and the footer.
Exercise
Why is "Reviewed by: Sam" a poor footer, and what should it be?
Show answer
Footer tokens must use a hyphen instead of a space so they can be told apart from body text. It should be "Reviewed-by: Sam". Only BREAKING CHANGE may contain a space.