Writing good commits

Topic 4 of 4 in Team workflows7 min readBeginnerFree course

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

Anatomy of a good commit message. The subject line, fix(cart): include quantity in order total, is short (about 50 characters) and imperative. After a blank line, the body explains why: the total summed item prices only, so buying 3 of an item charged for 1; multiply price by quantity and add a test for multi-item carts. The line Fixes #42 links the issue and closes it on merge. Conventional Commits types: feat, fix, docs, refactor, test, chore, ci. Write “Add”, not “Added” or “Adds”: finish the sentence “If applied, this commit will…”

Text
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”).

Two kinds of line usually go at the end:

  • Closing keywords such as Fixes #42 or Closes #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: value lines 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
Terminal
git log -1 --format=%B | git interpret-trailers --parse

Keep 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.

Text
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 endpoints

The 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 calc to calculateTotal” and “Add tax to total” are two commits — even if you did them together. Use git add -p to 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 bisect and 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 -i to 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:

Terminal
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 ~/.gitmessage

Lines starting with # are removed from the final message.

The test of a good commit: someone who has never seen this code reads git log -p for this commit and understands what changed and why — without asking you.

Exercise

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.

Terminal
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 commit

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; put Fixes #42 in 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.