Sub topic 4
Adding a scope for context
This chapter assumes you can write a basic commit with git commit, know the type: description form from the previous chapters, and can run commands in a terminal. It covers the optional scope: what it is, how to choose one, and how to keep scopes consistent.
What a scope is
A scope is a noun in parentheses placed right after the type. It names the part of the codebase the commit touches. The type says what kind of change you made. The scope says where.
fix(parser): handle empty input without throwing
feat(api): add pagination to the list endpoint
docs(readme): explain the install stepsThe specification calls the scope optional. If a change does not belong to one area, leave it out and write fix: prevent crash when the config file is empty. Do not invent a scope just to fill the slot.
Where the scope goes
The scope sits between the type and the colon. A breaking-change ! goes after the closing parenthesis.
feat(api)!: remove the deprecated /v1/users endpointThe order is always type(scope)!: description. Writing feat!(api): ... is a common mistake. It does not match the format, and the hook in the enforcement chapter will reject it.
Choosing a scope
Pick a scope that a reader of the history already understands. Good scopes usually come from one of these sources:
- A module or package:
parser,auth,cli. - A layer:
api,ui,db. - A feature area:
checkout,search. - A package name in a monorepo:
web,core.
Use one noun. The spec says a scope is a noun describing a section of the codebase, so avoid sentences, ticket numbers, and file paths with many segments. Ticket numbers belong in a footer such as Refs: #482.
A useful test: if someone filters the log by your scope, should this commit appear? If yes, the scope fits.
Searching by scope
Because the scope is part of the first line, plain git log can filter on it.
git log --oneline --grep='^feat(parser)'
git log --oneline --grep='(api)'The first command lists only features in the parser. The second lists every commit that mentions the api scope, whatever its type. In a regular expression, parentheses are literal characters in basic --grep patterns. If you switch to extended mode with -E, escape them as \( and \).
Keeping scopes consistent
The format does not limit which scopes you use. That freedom causes drift. A team can end up with auth, authentication, and login for the same area, and then the filters above miss commits.
To prevent that:
- Agree on a short list of scopes and write it in your contributing guide.
- Use lowercase, because
APIandapiare different strings togrep. - Prefer the shorter, stable name.
- Review the list when the code structure changes.
Tooling can enforce the list. The commitlint configuration from the enforcement chapter has a scope-enum rule that rejects scopes outside an allowed set. The hook pattern shown earlier already restricts scopes to lowercase letters, digits, dots, underscores, and hyphens through [a-z0-9._-]+.
Common mistakes
| Mistake | Why it is a problem | Better |
|---|---|---|
| `fix(PARSER): ...` | Mixed case breaks filtering and lint rules | `fix(parser): ...` |
| `fix(parser) handle empty input` | Missing colon | `fix(parser): handle empty input` |
| `feat(add search box): ...` | A sentence, not a noun | `feat(search): add search box` |
| `fix(#482): ...` | A ticket is not a code area | `fix: ... ` with `Refs: #482` |
| `feat!(api): ...` | `!` is in the wrong place | `feat(api)!: ...` |
Scope and release notes
Changelog tools commonly group entries by scope, so a clear scope improves the generated notes. Several feat(api) entries then appear together under "api". This only works when scope names are stable, which is another reason to keep the list short.
Exercise
A commit adds a retry option to the command-line tool and its description is "add retry flag". The code lives in the cli module, and the change removes an old flag, so it is breaking. Write the full first line.
Show answer
feat(cli)!: add retry flag. The type is feat, the scope is cli in parentheses, and the ! goes after the closing parenthesis and before the colon. A BREAKING CHANGE footer can add detail about the removed flag.
Exercise
Your team has commits scoped auth, authentication, and login, all for the same module. Name one problem this causes and one way to prevent it.
Show answer
A filter such as git log --grep='^fix(auth)' misses the other two spellings, and grouped changelogs split the area into three sections. Prevent it by agreeing on one name, documenting it, and enforcing the allowed list with commitlint's scope-enum rule.