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.
_10Fix double charge when checkout is retried_10_10The payment request had no idempotency key, so a retry after a_10timeout created a second charge. Send the order ID as the key so_10the provider ignores duplicates._10_10Fixes #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
- Separate the subject from the body with a blank line.
- Keep the subject to about 50 characters, and treat 72 as a hard limit.
- Capitalise the subject.
- Do not end the subject with a full stop.
- Use the imperative mood: "Add", "Fix", "Remove", not "Added" or "Fixes".
- Wrap the body at about 72 characters.
- 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.
_10git 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.
_10git 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.
_10git add -p_10git 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.
_10feat(cart): add saved-for-later list_10fix(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.
Add trailers for links and credit
Lines at the end of the body in the form Key: value are called trailers. Hosts and tools understand several of them.
_10Fixes #482_10Co-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.
Related reading
#Was this resource helpful?