For developers

How to write a good commit message (with examples)

A good commit message says what changed and why in a way your future self can search. Here is the common structure, seven practical rules and before-and-after examples.

Cursorlane Editorial TeamSoftware & tools 5 min read
Top-down view of a desk with a laptop, a stack of labelled-looking but blank file boxes and handwritten notes

A good commit message explains what a change does and why it was needed, in a short summary line followed, when useful, by a few lines of detail. It takes a minute to write and can save hours later when someone, often you, is trying to understand why the code looks the way it does.

This guide covers why commit messages matter, the widely used structure, practical rules with examples, and how teams use conventions to keep history consistent.

Why do commit messages matter?

Code shows what the program does now. It rarely shows why a decision was made, which alternatives were rejected, or which bug a strange-looking line is protecting against. The commit history is where that reasoning lives, attached to the exact lines it explains.

When you run git blame on a confusing line or search the history for when a bug appeared, the messages are what you read. 'Fix stuff' and 'wip' tell you nothing. 'Retry payment request once on timeout to avoid duplicate charges' tells you exactly what you need, months later.

Good messages also make code review faster, help tools generate release notes, and make it far easier to revert a single change safely when something goes wrong in production.

What is the standard structure?

The convention used by Git itself and most open-source projects has three parts. The first line is a short summary, ideally about 50 characters and no more than about 72, because many tools show only that line. Then a blank line. Then an optional body, wrapped at about 72 characters, that explains what and why.

Here is an example. Summary: 'Validate email format on signup form'. Body: 'Users could submit addresses without a domain, which caused bounced welcome emails and support tickets. Add a basic format check on the client and the same check on the server. Closes the related issue in the tracker.'

The body is not always needed. A typo fix or a version bump can stand on a summary line alone. Anything that involves a decision, a trade-off or a bug with a non-obvious cause deserves a body.

Write for a reader who has none of your current context.

What rules make messages clearer?

These rules are widely followed because they make history easy to scan and read in any tool.

  • Write the summary in the imperative mood: 'Add', 'Fix', 'Remove', not 'Added' or 'Fixes'. Read it as 'If applied, this commit will…'.
  • Keep the summary short and specific; leave out the final full stop.
  • Capitalise the first word of the summary, unless your team's convention says otherwise.
  • Separate the summary from the body with a blank line.
  • Use the body to explain why the change was needed and what it affects, not a line-by-line list of edits.
  • Mention issue or ticket references in the body or footer.
  • Make one logical change per commit, so one message can describe it honestly.

What do weak and strong messages look like?

Most weak messages fail in the same way: they describe the act of editing rather than the effect of the change. Compare these pairs.

Seeing them side by side makes the difference clear. The strong versions would still make sense to someone who has never seen the code.

Notice that none of the strong examples mention how long the work took, which files were touched or who asked for it. The diff already shows the files, and the tracker holds the conversation. The message should add the one thing neither of those can: a clear statement of intent that still makes sense when read on its own in a long list of history.

  • Weak: 'fix bug'. Strong: 'Prevent crash when profile photo is missing'.
  • Weak: 'update styles'. Strong: 'Increase button contrast to meet accessibility guidelines'.
  • Weak: 'changes'. Strong: 'Move date parsing into shared helper'.
  • Weak: 'address review comments'. Strong: 'Rename userList to activeUsers for clarity'.
  • Weak: 'wip'. Strong: squash or rewrite work-in-progress commits before merging.

What are Conventional Commits?

Some teams use a lightweight convention called Conventional Commits, where the summary starts with a type and an optional scope, such as 'feat(auth): add passkey sign-in' or 'fix(cart): keep quantity when item is re-added'. Common types include feat, fix, docs, refactor, test and chore.

The structure lets tools generate changelogs automatically and decide how to bump version numbers under semantic versioning. A footer starting with 'BREAKING CHANGE:' flags a change that is not backwards compatible.

It is a team choice, not a rule of Git. If your project uses it, follow it consistently. If not, plain imperative summaries work perfectly well.

How do you build the habit?

Small commits make good messages easy. If you struggle to summarise a commit in one line, it probably contains more than one change. Split it using git add with specific files, or git add -p to stage individual chunks of a file.

Write messages in your editor rather than on the command line when a body is needed: running git commit without -m opens your configured editor with room for a summary and body. Before opening a pull request, read through your branch's history with git log --oneline and tidy up vague messages while the change is fresh in your mind.

Finally, write for a reader who has none of your current context. That reader might be a new colleague, a reviewer in another time zone, or you in a year's time with no memory of this afternoon.

Frequently asked questions

How long should a commit message be?

+

Keep the summary line to about 50 characters and no more than about 72. The body can be as long as needed, wrapped at about 72 characters per line. Most commits need a few sentences at most.

Should commit messages be in past or present tense?

+

The common convention is the imperative mood, such as 'Fix login redirect'. It matches the messages Git generates itself, like 'Merge branch …'. Follow your team's style if it differs.

How do I change the message of my last commit?

+

Run git commit --amend and edit the message. Only do this before you push, or on a branch no one else uses, because it rewrites the commit.

Do I need to mention ticket numbers in commit messages?

+

It is helpful if your team uses an issue tracker, because it links code to the discussion behind it. Put the reference in the body or footer so the summary stays readable.

What does squashing commits mean?

+

Squashing combines several commits into one, often used to tidy up work-in-progress commits before merging. You can do it with an interactive rebase or with a squash-merge option on many code hosting platforms.

Should I mention the files I changed in the commit message?

+

Usually not. Git already records exactly which files and lines changed, and anyone can see them with git show. Use the message for what the diff cannot show: the purpose of the change and any reasoning behind it.

Next time you commit, ask what someone reading the history in a year would need to know. Put that in a short imperative summary and, when it matters, a few lines of why.

How we research: Guides are written from established sources and checked for accuracy. They are general information, not professional advice.

Written by

Cursorlane Editorial Team

Software & tools

Our writers are developers and heavy software users who test what they write about and update guides when the tools change.

More Stories

View all →