Writing good commit messages

Beginner · 11 min

Write commit messages that still make sense in six months: a short imperative subject, a body that explains why, and one idea per commit.

Before thisYour first repository and commit

Cameron McKenzie · 11 min

Writing good commit messages

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cameron McKenzieReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A commit message is read far more often than it is written, mostly in git log --oneline, where only the subject line shows.
  • Write the subject as an instruction to the codebase, in the imperative mood, short enough to fit on one line.
  • Use the body to explain why the change was made. The diff already shows what changed.
  • One commit should hold one idea, which is what keeps the message short and the history easy to search.
  • git commit -m twice, or git commit with no -m, lets you write a subject and a body.

Who reads a commit message

You do, later. Six months from now tinynotes has odd behaviour in how it counts lines. You run git log hoping to find the change that caused it. You see a column of messages, and you have to decide from those few words which commit to open.

Here is what that column looks like if you aren’t careful. This is a scratch repository I made to show it:

$ git log --oneline
cca19f1 Fixed bugs and also added count and renamed things and updated the README for the new commands
1d1b229 Fix crash when notes.txt does not exist
d63c3bb wip
534b8ac fix
a03242f stuff

Which commit renamed things? Which fix was it? stuff, fix and wip carry no information. The long one carries too much: it tells you five things happened but hides which diff belongs to which. Only the fourth line, “Fix crash when notes.txt does not exist”, would help future you. And searching works on it:

$ git log --oneline --grep=crash
1d1b229 Fix crash when notes.txt does not exist

The shape of a good message

Git’s own convention, used across most projects:

Add a count command

Print how many notes are stored in notes.txt. An empty or missing
file is not handled yet; that is a known gap for the next change.
  • A subject line. Short, ideally under about 50 characters, no full stop needed. It is what --oneline shows, so write it to stand alone.
  • A blank line. Tools use it to tell the subject from the body. Skip it and some of them mash the two together.
  • A body, when the change needs one. Wrapped at around 72 characters so it reads well in a terminal.

The 50 and 72 are habits, not limits Git enforces. Git accepted my long subject above without complaint.

Imperative mood

Write the subject as an order: Add, Fix, Rename, Remove. A test: the subject should finish the sentence “If applied, this commit will …”. “Add a count command” works. “Added a count command” and “Adds a count command” don’t. Git itself writes its own automatic messages this way, for example “Merge branch ‘x’”. It reads oddly at first, since you’re describing something that already happened, but the message describes what applying the commit does, and that’s the useful question when you’re reading a list of changes.

The body explains why

The diff shows what changed. It can’t show why you chose this approach, what failed first, or what the next reader should know. That is the body’s job. For a one-line typo fix, no body is fine. For anything where a reviewer might ask “why did you do it this way?”, answer it in the message now, because the commit stays attached to the code forever and a chat thread doesn’t.

How to write one

For a one-liner, -m is fine. Pass -m twice to get a subject and a body, since each -m becomes a paragraph:

$ git commit -m "Add a count command" -m "Print how many notes are stored in notes.txt. An empty or missing
file is not handled yet; that is a known gap for the next change."
[main 8b1866b] Add a count command
 1 file changed, 3 insertions(+)

Or run git commit with no -m and Git opens your editor on a template. With -v added, the template also includes the diff you’re about to commit, which is a good way to re-read your change as you describe it. Git ignores every line starting with #, and an empty message aborts, which I confirmed by feeding it an editor that produced nothing: “Aborting commit due to empty commit message.”

git log -1 shows the result, with the body indented under the subject:

commit 8b1866b707cc27f0844f008b6ce2a74dea7da3a4
Author: Ada Example <ada@example.com>
Date:   Sat Jan 10 10:00:00 2026 +0000

    Add a count command

    Print how many notes are stored in notes.txt. An empty or missing
    file is not handled yet; that is a known gap for the next change.

Small commits make messages easy

If you can’t write a subject without “and”, you probably have two commits. The cure is upstream of the message: stage precisely with git add -p, so each commit holds one idea, and the message writes itself.

Teams sometimes add a stricter format on top, with prefixes such as feat: and fix: that tools can read. That scheme is called Conventional Commits and has its own lesson later in this roadmap. Start with the plain habits above, because they apply with or without a prefix.

What the video covers

Cameron McKenzie’s “How to Write a Git Commit Message: Conventions & Best Practices” runs about eleven minutes and was published around 2024. I had no transcript, so this comes from the title and description, which cover conventions and best practices for commit messages. The tags mention several hosting sites, so expect it to be general rather than GitHub-specific. It is in English. Whether it matches the subject-and-body shape above, I couldn’t confirm. The Modern SDE’s lecture on the same topic is linked as a second view.

Where this fits

Your history will now be readable. Not everything in your folder belongs in it, though. Next: ignoring files with .gitignore.

Check yourself

  1. 01Why does the subject line matter more than the body?

    git log --oneline, many Git tools and pull request lists show only the subject, so that line has to carry the meaning on its own.

  2. 02What does the imperative mood mean here, and what is a quick test for it?

    Phrase the subject like a command, such as Add a count command. A test: it should complete the sentence 'If applied, this commit will ...'.

  3. 03If the diff shows what changed, what should the body of the message say?

    Why: the reason for the change, what you considered, and anything the next reader needs that the code doesn't show.

  4. 04A commit message needs the word 'and' three times to describe it. What does that suggest?

    The commit holds several ideas. Split it into separate commits with git add -p.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark