Writing good commits
Overview
Code tells you what the program does. Commit history tells you why it does it — why this line exists, what bug that if fixed, which decision led here. Good commits make that history useful: for reviewers today, and for whoever runs git blame in two years (often you).
The anatomy of a commit message
fix(cart): include quantity in order total
The total summed item prices only, so buying three of an item
charged for one. Multiply each price by its quantity and add a
test for carts with multiple items.
Fixes #42
Co-authored-by: Ben Ito <ben@example.com>Subject line
- Imperative mood: “Add”, “Fix”, “Remove” — it should complete “If applied, this commit will…”. That matches Git’s own messages (“Merge branch…”, “Revert…”).
- Short: around 50 characters, 72 at most. It’s what
git log --oneline, GitHub and email subjects show. - Specific: “Fix crash when cart is empty”, not “Fix bug”.
- No full stop at the end.
Body
After a blank line (Git uses it to separate subject from body), explain:
- why the change was needed — the problem, the bug, the requirement;
- what changed at a high level, especially anything surprising;
- side effects, trade-offs, or alternatives you rejected.
Wrap lines at about 72 characters so it reads well in terminals. Skip the body for truly obvious changes (“Fix typo in README”).
Issue links and trailers
Two kinds of line usually go at the end:
- Closing keywords such as
Fixes #42orCloses #42. GitHub links the issue and closes it when the commit reaches the default branch. GitHub finds them anywhere in the message; by convention they sit just above the trailers. - Trailers:
Key: valuelines forming the last paragraph, which Git itself understands:
| Trailer | Meaning |
|---|---|
Co-authored-by: Name <email> |
credit a pair-programming partner — GitHub shows both avatars |
Signed-off-by: Name <email> |
certify you have the right to submit it (DCO); added by git commit -s |
Reviewed-by:, Refs: |
used by some projects |
git log -1 --format=%B | git interpret-trailers --parseKeep Fixes #42 in its own paragraph rather than mixing it into the trailer block: it isn’t in Key: value form, and a block containing a non-trailer line may not be recognised as trailers at all.
Conventional Commits
Many teams use the Conventional Commits format: a type, an optional scope, and the subject.
feat(search): add filter by price
fix(cart): include quantity in total
docs: describe the release process
refactor(api): extract price formatting
test(cart): cover empty carts
chore(deps): update express to 5.1
ci: cache npm dependencies
feat(api)!: remove the v1 endpointsThe common types: feat (new feature), fix (bug fix), docs, style (formatting only), refactor (neither feature nor fix), perf, test, build, ci, chore. A ! after the type — or a BREAKING CHANGE: trailer — marks a breaking change.
Benefits:
- history is scannable — you can see at a glance what’s a feature and what’s a fix;
- tools can generate changelogs and choose the next version automatically:
fix→ patch,feat→ minor, breaking → major (Tags and releases); - a commit-msg hook or CI check can enforce the format (Git hooks).
Use it if your team agrees to; consistency matters more than the particular convention.
What goes in one commit
The message is only half of it. The content of each commit should be:
- One logical change. “Rename
calctocalculateTotal” and “Add tax to total” are two commits — even if you did them together. Usegit add -pto split mixed work. - Complete. Code, tests and docs for that change, together.
- Working. The project builds and the tests pass at every commit. That keeps
git bisectand reverting useful. - Free of noise. Separate formatting and whitespace changes from behaviour changes, so the real change is visible in the diff.
When to polish
- While working: commit freely; messy messages like “wip” are fine on your branch.
- Before review: use
git rebase -ito reword, squash fixups and split commits (Interactive rebase). - If the team squash-merges: the PR title and description become the commit on
main— so put the effort into those instead.
A message template
Remind yourself of the structure every time:
cat > ~/.gitmessage <<'EOF'
# <type>(<scope>): <subject> (imperative, ~50 chars)
# Why is this change needed? What does it change?
# Fixes #
EOF
git config --global commit.template ~/.gitmessageLines starting with # are removed from the final message.
The test of a good commit: someone who has never seen this code reads
git log -pfor this commit and understands what changed and why — without asking you.
Rewrite a messy history into clear commits
The script creates a branch with three badly described commits: “stuff”, “fix” and “more changes”. Rewrite all three messages with an interactive rebase into Conventional Commits with useful subjects: feat(cart): show item quantities, fix(cart): include quantity in total and docs: describe the cart. Give the fix commit a body explaining why, a Fixes #42 line so GitHub closes the issue, and a Co-authored-by trailer for Ben. Check the trailer with git interpret-trailers --parse.
mkdir commits && cd commits && git init -qb main
echo "cart" > cart.js && git add . && git commit -qm "Start cart"
git switch -qc feature/quantities
echo "cart + qty display" > cart.js && git commit -qam "stuff"
echo "total = price * qty" > total.js && git add . && git commit -qm "fix"
echo "# Cart" > CART.md && git add . && git commit -qm "more changes"
# 1. Rewrite the three messages (git rebase -i main, mark each as reword)
# 2. Check the history and the trailers on the fix commitmkdir commits && cd commits && git init -qb main
echo "cart" > cart.js && git add . && git commit -qm "Start cart"
git switch -qc feature/quantities
echo "cart + qty display" > cart.js && git commit -qam "stuff"
echo "total = price * qty" > total.js && git add . && git commit -qm "fix"
echo "# Cart" > CART.md && git add . && git commit -qm "more changes"
# 1. Rewrite the three messages
# By hand: git rebase -i main, change each "pick" to "reword", save,
# then type the new message in the editor each time.
# Scripted equivalent:
cat > /tmp/msg-stuff <<'EOF'
feat(cart): show item quantities
EOF
cat > /tmp/msg-fix <<'EOF'
fix(cart): include quantity in total
The total summed item prices only, so buying three of an item
charged for one. Multiply each price by its quantity.
Fixes #42
Co-authored-by: Ben Ito <ben@example.com>
EOF
cat > /tmp/msg-more <<'EOF'
docs: describe the cart
EOF
GIT_SEQUENCE_EDITOR='sed -i.bak "s/^pick/reword/"' \
GIT_EDITOR='f() { case "$(head -1 "$1")" in stuff) cp /tmp/msg-stuff "$1";; fix) cp /tmp/msg-fix "$1";; "more changes") cp /tmp/msg-more "$1";; esac; }; f' \
git rebase -i main
# 2. Check the history and the trailers on the fix commit
git log --oneline main..HEAD
git log -1 --format=%B HEAD~1 | git interpret-trailers --parse
# Co-authored-by: Ben Ito <ben@example.com>Summary
- A good message has a short imperative subject, a blank line, and a body explaining what and why.
- Trailers in the last paragraph —
Co-authored-by:,Signed-off-by:— are machine-readable metadata; putFixes #42in its own paragraph above them. - Conventional Commits (
type(scope): subject) make history scannable and enable automatic changelogs and versions. - Each commit should be one logical, working change: separate refactors, formatting and features.
- Clean up messages and structure with interactive rebase before sharing.