Sub topic 4
Adding a scope for context
This chapter assumes you can write a basic conventional commit (type, colon, description), know what the common types mean, and have a working idea of Semantic Versioning. It covers the optional scope: what it is, how to choose one, and where scopes go wrong.
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.
fix(parser): handle trailing commas in object literalsHere fix is the type and parser is the scope. The type answers "what kind of change is this?" The scope answers "where does it apply?"
The Conventional Commits 1.0.0 specification says a scope MAY be provided after a type. It must be a noun describing a section of the codebase, surrounded by parentheses, for example feat(parser):. The spec does not define a list of scopes. Your project decides what they are.
A scope is optional. A commit without one is still valid:
fix: prevent crash when the config file is emptyWhere the scope goes
The scope sits between the type and the colon. If the commit also carries a breaking-change marker, the ! goes after the closing parenthesis and before the colon.
feat(api)!: remove the deprecated /v1/users endpointThe order is always: type, optional scope, optional !, colon, space, description.
| Written | Valid? | Reason |
|---|---|---|
| `feat(parser): add array literals` | Yes | Type, scope, colon, space, description |
| `feat: add array literals` | Yes | Scope is optional |
| `feat(parser)!: drop old syntax` | Yes | `!` follows the scope |
| `feat!(parser): drop old syntax` | No | `!` must come right before the colon |
| `feat (parser): add array literals` | No | No space is allowed before the parenthesis |
| `feat(parser):add array literals` | No | A space is required after the colon |
What a scope buys you
A scope adds context without making the description longer. Compare these two headers:
fix: handle null user in the session lookup
fix(auth): handle null user in the session lookupThe second tells a reviewer which area to open before they read the diff. It also makes history easier to filter. Because the scope is part of the first line, plain git log can search for it:
git log --oneline --grep='^fix(auth)'
git log --oneline --grep='(parser)'The first command lists only fixes in the auth area. Note that --grep takes a basic regular expression by default, and in that syntax plain parentheses are literal characters, so these patterns match the text as written.
Release tools can also use scopes. Changelog generators often group entries by scope, so a release note can show a "parser" heading with its features and fixes beneath it.
Choosing scope names
The spec says only that a scope is a noun naming a section of the codebase. That leaves a lot of room, so teams need to agree on rules. These are the common sources for scope names.
- Modules or packages. In a monorepo, use the package name:
feat(cli):,fix(core):. This is the most common and the least ambiguous choice. - Directories. Use the top-level folder:
fix(auth):,perf(db):. - Features or domains. Use the product area:
feat(checkout):,fix(search):. - Layers. Use
api,ui, ordbwhen the project is organised by layer.
Pick one approach and stay with it. A project that mixes package names, feature names, and layer names ends up with scopes that overlap, and then the filter is unreliable.
Rules that keep scopes useful
- Use a noun.
parseris a scope.fixing-parseris not. - Keep it short and lowercase.
auth, notAuthentication Service. Lowercase avoids duplicates such asAPIandapi. - Use one name per area. Decide between
dbanddatabaseand write the choice down. - Keep the set small. A dozen well-known scopes help. Hundreds of one-off scopes do not.
- Do not use an issue number as a scope.
fix(#482):is a reference, not an area. Put it in a footer such asRefs: #482. - Do not use a scope that repeats the type.
docs(docs):adds nothing.
Example: scopes for a small web project
Suppose the repository has src/auth, src/billing, src/ui, and docs. A scope list might be:
auth, billing, ui, deps, ciThen these commits read clearly:
feat(billing): add proration for mid-cycle plan changes
fix(ui): keep the modal focus trap active after resize
build(deps): bump the http client to 4.2.1
ci: cache node_modules between workflow runsThe last commit has no scope. That is fine, since ci already says everything the scope would.
When to leave the scope out
Leave the scope off when it does not add information.
- The type already names the area, as with
ci:ordocs:. - The change is small and project-wide, such as a formatting pass.
- The project has only one component, so every commit would carry the same scope.
A scope that appears on every commit with the same value is noise. Likewise, do not force a scope when none fits. A missing scope is better than a made-up one.
Commits that touch several areas
Sometimes a change spans more than one area. You have three sensible options.
- Split the commit. If the changes are independent, make two commits, each with its own scope. This gives the cleanest history.
- Use the dominant scope. If one area is the real subject and the others are consequences, name that one.
- Omit the scope. If no single area dominates, a scope-free header is honest.
Some tools accept several scopes separated by a comma, as in feat(api,cli):. The specification does not define this form. It only says a scope is a noun in parentheses. Whether api,cli is accepted depends on the tool reading it, and some parsers treat the whole string as one scope. Avoid multiple scopes unless your tooling documents support for them.
Scopes and Semantic Versioning
A scope has no effect on the version bump. The bump depends on the type and on whether the commit is a breaking change:
fixleads to a PATCH release.featleads to a MINOR release.- Any commit marked as breaking leads to a MAJOR release.
So feat(parser): and feat(cli): both cause a MINOR bump. The scope only labels the change.
Enforcing scopes
A scope convention that nobody checks drifts. There are two levels of enforcement.
Format only
The commit-msg hook from the enforcement chapter already allows an optional scope. Its pattern includes this fragment:
(\([a-z0-9._-]+\))?This accepts any scope made of lowercase letters, digits, dots, underscores, and hyphens. It checks the shape, not the meaning, so fix(asdf): passes.
An allowed list
To limit scopes to a known set, tighten the pattern. This script is a complete hook that accepts only the scopes auth, billing, ui, deps, and ci:
#!/bin/sh
# .git/hooks/commit-msg (must be executable)
types='feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert'
scopes='auth|billing|ui|deps|ci'
pattern="^($types)(\(($scopes)\))?!?: .+"
if ! head -n 1 "$1" | grep -Eq "$pattern"; then
echo "Commit message must look like: type(scope)!: description" >&2
echo "Allowed scopes: $scopes" >&2
exit 1
fiWith commitlint, the @commitlint/config-conventional ruleset can be extended with a scope-enum rule:
# commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'scope-enum': [2, 'always', ['auth', 'billing', 'ui', 'deps', 'ci']],
'scope-case': [2, 'always', 'lower-case'],
},
};In a rule array, the first number is the severity (2 is an error, 1 a warning, 0 disabled). The second value says whether the rule applies (always) or is negated (never). The third is the rule's value. Because scope-enum only applies when a scope is present, commits without a scope still pass. To require a scope on every commit, add 'scope-empty': [2, 'never'].
Trade-offs of a closed list
A closed list keeps history tidy but adds friction. Every new module needs a config change before anyone can commit to it. For a small team with a stable layout this is cheap. For a large or fast-changing repository, you may prefer format-only checking plus a review habit.
Common mistakes
- Using the scope as a second description.
fix(fix-null-crash-in-login):is a sentence, not a noun. - Putting the scope in the description.
fix: auth - handle null userloses the filter benefit. Usefix(auth): handle null user. - Inconsistent names.
db,database, andDBall appear in one history. - Mixing up scope and type.
feat(fix):says nothing useful. The type is what kind of change, the scope is where. - Renaming a module and leaving old scopes behind. If
authbecomesidentity, decide when old and new scopes both appear in the log, and note the change in your contributing guide. - Capital letters and spaces.
fix(Auth Service):is hard to type and hard to search, and it usually fails linting.
Squash merges
When a pull request is squash-merged, the final commit message is often taken from the PR title. If your project uses scopes, the PR title should carry one too, such as feat(billing): add proration for plan changes. A later chapter covers this workflow in detail.
Summary of decisions
| Question | Guidance |
|---|---|
| Is a scope required? | No, it is optional in the spec |
| What is it? | A noun naming a section of the codebase |
| Where does it go? | In parentheses between the type and the colon |
| Does it change the version bump? | No, only the type and breaking marker do |
| How many scopes per commit? | One, unless your tooling documents more |
| How to restrict names? | A hook pattern or commitlint `scope-enum` |
Exercise
A repository has the packages core, cli, and web. A developer fixes a crash in the cli package that happened when the --output flag was missing. Write the commit header, then say what version bump it causes. Finally, the same developer also renames a public function in core, which breaks callers. Write that header too.
Show answer
The first header is fix(cli): handle missing --output flag without crashing. A fix causes a PATCH bump, and the scope cli does not change that. The second header is refactor(core)!: rename loadConfig to readConfig, with a footer such as BREAKING CHANGE: callers must use readConfig instead of loadConfig. The ! goes after the closing parenthesis and before the colon. Because it is marked breaking, it causes a MAJOR bump regardless of the type. These two changes should be separate commits, since they affect different packages.
Exercise
Your commit-msg hook uses the pattern ^(feat|fix)(\([a-z]+\))?: .+. Which of these headers pass: fix(auth): handle null user, fix(Auth): handle null user, feat(api-v2): add route, feat: add route? Explain each result.
Show answer
fix(auth): handle null user passes, since auth is all lowercase letters. fix(Auth): handle null user fails because the scope class [a-z]+ does not allow a capital A. feat(api-v2): add route fails because the class has no hyphen or digit. feat: add route passes, since the whole scope group is optional. To allow api-v2, change the class to [a-z0-9._-]+.