More Git answers

by Danny
Published 2 min read

A good commit message has a short imperative subject line, a blank line, and a body that explains why the change was made. Here is the format, examples, and how to write it without -m.

The short answer

#

Write a short subject line in the imperative mood, about 50 characters, with no full stop. Leave a blank line. Then write a body, wrapped at around 72 characters, that explains why the change was needed and anything a reviewer would not see from the diff.


_10
Fix double charge when checkout is retried
_10
_10
The payment request had no idempotency key, so a retry after a
_10
timeout created a second charge. Send the order ID as the key so
_10
the provider ignores duplicates.
_10
_10
Fixes #482

Why it happens

#

The diff already shows what changed. What it cannot show is why: the bug you saw, the option you rejected, the constraint you were working around. Six months later, someone running git blame or git log on that line needs exactly that context, and often that someone is you.

The format is not arbitrary either. Many Git tools treat the first line as a title. git log --oneline, git shortlog, git rebase -i, email patches and pull request lists all show only the subject, and some truncate it. The blank line is how Git separates the subject from the body.

How to fix it

#

Follow the seven conventions

  1. Separate the subject from the body with a blank line.
  2. Keep the subject to about 50 characters, and treat 72 as a hard limit.
  3. Capitalise the subject.
  4. Do not end the subject with a full stop.
  5. Use the imperative mood: "Add", "Fix", "Remove", not "Added" or "Fixes".
  6. Wrap the body at about 72 characters.
  7. Use the body to explain what and why, not how.

A quick test for the imperative mood: the subject should complete the sentence "If applied, this commit will ...". "If applied, this commit will fix double charge when checkout is retried" reads correctly. It also matches the messages Git writes itself, such as "Merge branch" and "Revert".

Write the message in your editor, not with -m

git commit -m makes it tempting to write a one-line message. Run git commit on its own and Git opens your editor, where a subject and body are easy to write. Set the editor once.


_10
git config --global core.editor "code --wait"
_10
# or: nano, vim, "zed --wait", and so on

If you prefer the command line, each -m becomes a separate paragraph.


_10
git commit -m "Fix double charge when checkout is retried" \
_10
-m "The payment request had no idempotency key, so a retry after a timeout created a second charge."

Keep each commit to one logical change

A good message is much easier when the commit does one thing. If your subject needs "and", consider splitting it. git add -p lets you stage parts of a file so you can commit related changes separately.


_10
git add -p
_10
git commit

Use Conventional Commits if your team does

Some projects prefix the subject with a type, so tools can generate changelogs and version numbers. The common types are feat, fix, docs, refactor, test and chore, with an optional scope in brackets.


_10
feat(cart): add saved-for-later list
_10
fix(checkout): prevent double charge on retry

In this style the description usually starts with a lower-case letter. Follow whatever your project already does; consistency matters more than either style.

Lines at the end of the body in the form Key: value are called trailers. Hosts and tools understand several of them.


_10
Fixes #482
_10
Co-authored-by: Sam Lee <[email protected]>

Common pitfalls

#
  • Vague subjects. "Fix bug", "Update", "WIP" and "changes" tell readers nothing. Name the behaviour that changed.
  • Describing the code instead of the reason. "Change timeout to 30" is in the diff. "Raise timeout because the export job takes 20 seconds" is not.
  • Very long subjects. They are cut off in most tools. Move the detail to the body.
  • Relying only on an issue link. Links break and trackers move. Summarise the reason in the message itself.
  • Leaving "fix typo" commits in a pull request. Squash or fix them up before merging so the history stays readable.
#

Was this resource helpful?