Sub topic 2
The structure: type, scope, description, body, footers
This chapter assumes you can use git add, git commit and git log. It also assumes you know what MAJOR.MINOR.PATCH means in Semantic Versioning and can run commands in a terminal. You will learn the grammar of a Conventional Commits 1.0.0 message, one part at a time. Later chapters cover how to choose types, write breaking changes, and enforce the format.
The overall shape
A conventional commit message has three regions: a header line, an optional body, and optional footers. The header is required. The body and footers are optional, but each must be separated from the part before it by one blank line.
<type>[optional scope][optional !]: <description>
[optional body]
[optional footer(s)]Git does not know about this layout. To Git, a commit message is text. The convention is a contract between people and tools, and the tools (linters, changelog generators, release scripts) parse that text.
A complete example
feat(parser)!: add support for array literals
The parser now accepts [1, 2, 3] syntax and produces an ArrayNode.
Nested arrays are supported up to 32 levels deep.
Reviewed-by: Dana
Refs: #482
BREAKING CHANGE: the parser no longer treats a leading [ as a plain token.Each part of this message is described below.
| Part | In the example | Required |
|---|---|---|
| Type | `feat` | Yes |
| Scope | `(parser)` | No |
| Breaking marker | `!` | No |
| Separator | `: ` (colon, then one space) | Yes |
| Description | `add support for array literals` | Yes |
| Body | The two sentences after the blank line | No |
| Footers | `Reviewed-by`, `Refs`, `BREAKING CHANGE` | No |
The type
The type is a noun at the very start of the message. It tells a reader and a tool what kind of change this is. The specification requires only two types and gives them meaning:
featadds a new feature to the codebase.fixrepairs a bug.
The specification allows other types. The common set (docs, style, refactor, perf, test, build, ci, chore, revert) comes from the Angular convention and from tools such as @commitlint/config-conventional, not from the core specification. The next chapter covers how to choose among them. For now, treat the type as a single word followed directly by the next piece of syntax, with no space before the colon or scope.
Types are case-insensitive in the specification. Pick lowercase and stay with it, because linters and git log --grep patterns are usually case-sensitive.
The scope
A scope is a noun in parentheses that names the section of the codebase the change touches. It comes straight after the type with no space.
fix(auth): reject expired refresh tokens
docs(readme): correct the install command
feat(api/users): add pagination to the list endpointThe scope is optional, and the specification does not limit its values. Each project defines its own, such as package names in a monorepo, module names, or layers like ui and db. Two practical rules help:
- Use a short, stable list of scopes. If
auth,authnandloginall appear, a search for one misses the others. - Leave the scope out when a change has no single home. A cross-cutting change with a forced scope is more misleading than a header without one.
A scope never changes the version bump. Only the type and the breaking-change marker do that.
The separator and the description
After the type, scope and optional ! comes a colon and one space. The specification calls this out because it is the most common syntax error. fix:crash and fix : crash are both invalid. fix: crash is valid.
The description is a short summary of the change, placed immediately after the separator. It has no format rules in the specification beyond being present, but good practice is:
- Use the imperative mood, as in "add", "remove" or "prevent", so it reads as "this commit will add...".
- Start with a lowercase letter, unless the first word is a proper noun or identifier.
- Do not end with a period.
- Keep the whole header near 50 characters and under 72, so it is not cut off in
git log --onelineand in hosting UIs.
Compare these two headers for the same change:
fix: fixed stuff in the config loader.
fix(config): prevent crash when the config file is emptyThe second says what the change does, where it happens, and reads well as a changelog line.
The body
The body gives context that does not fit in the header. It starts one blank line after the description. The specification places no limits on it: it is free-form and may have any number of paragraphs, and even blank lines inside it are fine.
Use the body to explain why the change was made, which approach you chose over others, and any side effects. The diff already shows what changed, so the body does not need to repeat it.
fix(cache): evict entries when the disk is more than 90% full
The previous check ran only at startup, so long-running processes could
fill the disk. Eviction now runs after every write. This adds roughly
2 ms to a write on spinning disks.A common gotcha: if you omit the blank line between header and body, Git treats the two lines as the subject. Tools that read only the first paragraph, such as git log --oneline and git shortlog, then show both lines joined. Parsers may also reject the message.
Footers
Footers carry metadata. They start one blank line after the body (or after the header, if there is no body). Each footer is a token, a separator, and a value.
- The token is a word. Spaces in it are replaced with hyphens, so you write
Reviewed-by, notReviewed by. - The separator is either
:(colon and space) or#(space and hash). - The value is any text. It may contain spaces and span several lines. A footer ends where the next valid token and separator begin.
fix(db): close connections after a failed migration
Closes #231
Reviewed-by: Dana
Co-authored-by: Sam <[email protected]>Closes #231 uses the # separator. Hosting platforms such as GitHub read it to close an issue when the commit reaches the default branch. The other two lines use the colon form.
There is one exception to the hyphen rule. The token BREAKING CHANGE keeps its space. It must be uppercase, and BREAKING-CHANGE is accepted as a synonym. A footer with this token marks a breaking change, and Chapter 7 covers it in detail.
How footers relate to Git trailers
Git has its own idea of footers, called trailers. Git can parse them for you, which is handy for checking what a tool would see.
git log -1 --format=%B | git interpret-trailers --parseGit recognizes only the : separator by default, so a Closes #231 footer does not show up in this output even though the Conventional Commits specification counts it as a footer. Do not rely on git interpret-trailers to validate footers in the # form.
Writing multi-part messages from the command line
Each -m option to git commit adds a paragraph, and Git separates paragraphs with a blank line. That makes -m convenient for the three-region layout:
git commit \
-m "feat(parser): add support for array literals" \
-m "The parser now accepts [1, 2, 3] syntax and produces an ArrayNode." \
-m "Refs: #482"The first -m becomes the header, the second the body, and the third the footer. If you want a footer without a body, you still need the blank line, so the second -m is the footer:
git commit \
-m "fix: prevent crash when the config file is empty" \
-m "Refs: #310"Without -m, Git opens your editor. The layout is then yours to type, with blank lines where the grammar needs them.
Common structural errors
| Message | Problem | Fix |
|---|---|---|
| `feat add search` | Missing colon | `feat: add search` |
| `feat:add search` | No space after the colon | `feat: add search` |
| `feat (ui): add search` | Space before the scope | `feat(ui): add search` |
| `fix: crash`, then body text on the next line | No blank line before the body | Insert one blank line |
| `Reviewed by: Dana` | Space in the footer token | `Reviewed-by: Dana` |
| `breaking change: drops v1` | Wrong case for the token | `BREAKING CHANGE: drops v1` |
Most of these failures are silent. Git accepts the commit, and the problem appears later, when a changelog tool skips the commit or computes the wrong version bump. That is the reason the later chapters on enforcement matter.
Exercise
Rewrite this message so it is a valid conventional commit with the type fix, the scope upload, a body explaining that the size limit was 1 MB instead of 10 MB, and a footer that references issue 77 using the " #" separator. Original: "Fixed the upload limit."
Show answer
One valid answer: fix(upload): raise the file size limit to 10 MB The limit was set to 1 MB because of a typo in the constant, which rejected valid files. Closes #77 The header has the type, scope, colon, space and an imperative description with no final period. A blank line separates the body from the header, and another separates the footer from the body.
Exercise
A teammate writes the footer "Breaking change: drops Node 14" and the release tool does not bump the major version. What is wrong, and how do you fix it?
Show answer
The token "BREAKING CHANGE" must be uppercase (or the synonym BREAKING-CHANGE). The lowercase form is read as an ordinary footer token with no special meaning. Change it to "BREAKING CHANGE: drops Node 14", or add a ! before the colon in the header.