Sub topic 10
Writing commit messages that help later
This chapter assumes you can create a repository with git init, stage files with git add, and run git commit. You should also be able to read git log --oneline. Here you will learn what makes a commit message useful months later, and which mistakes make history hard to read.
Why the message matters
A commit records a snapshot, but the snapshot does not say why you took it. The diff shows what changed. The message must explain the intent. When you hunt for a bug six months from now, the message is often the only clue to why a line changed.
The shape of a good message
A commit message has two parts, separated by a blank line:
- A subject line: a short summary of the change.
- An optional body: the reason for the change and any context a reader needs.
Passing -m twice gives you both parts. Git places a blank line between the two messages.
git commit -m "Add usage notes to README" -m "Explain how to run the project locally."For longer bodies, run git commit with no -m. Git opens your editor, and you can write several paragraphs.
Rules for the subject line
- Keep it near 50 characters. Many tools truncate longer subjects.
- Use the imperative mood: "Add README", not "Added README" or "Adds README". Read it as completing the sentence "If applied, this commit will ...".
- Do not end it with a period.
- Describe one change. If you need the word "and", consider two commits.
What the body should say
The diff already shows what changed, so use the body for the reason. Explain the problem, why you chose this approach, and any side effects. Wrap lines at about 72 characters so they read well in a terminal.
The table compares weak and strong messages.
| Weak message | Stronger message |
|---|---|
| `fix` | `Fix crash when config file is missing` |
| `updates` | `Update README with install steps` |
| `Added stuff and fixed bugs` | Two commits, one per change |
| `WIP` | `Add login form validation` |
See the effect in the log
Messages pay off when you read history. Compare the output of git log --oneline for two repositories.
git log --onelineA useful history reads like a short changelog:
3f2a9c1 Fix crash when config file is missing
b81d4e7 Add usage notes to README
a09c552 Add READMEA poor history shows fix, more changes, and asdf, and you must open every diff to learn anything.
Common mistakes
- Vague subjects such as
fixorupdate. - Mixing unrelated changes in one commit, so no single message can describe it.
- Writing the body as a list of changed files. Git already records that.
- Using past tense in some commits and imperative in others. Pick one style and keep it, ideally the imperative.
A message workflow
Exercise
You staged a fix for a login timeout and also an unrelated typo correction in the README. Your draft message is "fixed stuff". Name two problems with it and say how to improve the commit.
Show answer
The subject is vague and in past tense, and it hides that two unrelated changes are mixed. Split the work into two commits, for example "Fix login timeout on slow networks" and "Correct typo in README". Add a body to the first commit if the cause of the timeout is not clear from the diff.
Exercise
Write the command that creates a commit with the subject "Ignore log files" and a body line "Log output changes on every run and should not be tracked."
Show answer
git commit -m "Ignore log files" -m "Log output changes on every run and should not be tracked."