Good commits and messages
Last updated
A history is only as useful as its commits and their messages. In this lesson you'll learn what makes a commit easy to review and undo, and the simple shape of a good message: a short command, a blank line, and why.
By the end of this lesson you can
- Keep each commit to one logical change, and commit often
- Write a subject line as a short command, under 50 characters
- Add a body that explains why, when the why isn't obvious
One logical change per commit
You saw in lesson 1.5 why one commit should hold one change. Two more habits make history easy to use:
- Commit often. A commit after each small step that works ("Add sinigang recipe", "Fix the steaming time") is easy to review and easy to undo. One commit at the end of a week is neither.
- Each commit should leave things working. For recipes that means no half-typed recipe in the middle of a commit called "Add halo-halo". For programs, it means the program still runs. Stage 8's bisect tool depends on this.
A useful test: could you undo this commit on its own without breaking anything else? If not, it probably holds more than one change.
The shape of a message
Git's own documentation suggests the shape most projects use: a short first line, a blank line, then more detail. Tim Pope set out the details in 2008 and Chris Beams wrote them up as seven rules in 2014:
- A subject line of about 50 characters or less, so it fits in
git log --onelineand on hosting sites. - Start with a capital letter, and no full stop at the end.
- Write it as a command: "Add pancit recipe", not "Added" or "Adds". A good test: it should finish the sentence If applied, this commit will…. Git writes its own messages this way: "Merge branch…", "Revert…".
- If there's more to say, leave a blank line, then a body that explains what and why, not how. The diff already shows how. Wrap the body at about 72 characters.
From the command line, each -m makes a paragraph, so the first is the subject and the second the body:
git commit -m "Use 1 tsp salt in the adobo" -m "Lola Remy measured it for us. The old line, a little salt, left everyone guessing." [main 3f2a9c1] Use 1 tsp salt in the adobo 1 file changed, 1 insertion(+), 1 deletion(-)
Most commits need only the subject. Write a body when someone reading later would ask "why?".
Rank the messages
Sort Ana's and Joaquin's messages by how useful they'd be to someone reading the history next year.
0 of 8 placed right
Add pancit bihon recipeCover the leche flan with foil while steamingfixed stuffUpdate adobo.mdAdded the sinigang recipe.add halo haloChange the salt in the adobo from a little salt to one teaspoon because Lola measured itwip
Rewrite Ana's first week
Here are three of Ana's real messages from her first week, and what each commit actually changed. Write a better subject line for each.
Ana wrote:
changesWhat the commit really did: Added Lola Remy’s sinigang recipe as sinigang.md.
0/50Ana wrote:
Fixed the thing Lola said about the flan.What the commit really did: Changed the leche flan steaming time from 30 to 40 minutes, after Lola Remy said the middle was runny.
0/50Ana wrote:
updateWhat the commit really did: Moved the README’s instructions for building the index page to the top.
0/50
Spot the trap: "fixed stuff"
In March, Lola Remy says the adobo tastes wrong. Ana opens git log --oneline to find what changed. The last month reads: fixed stuff, more fixes, update, stuff, changes.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-commit, Discussion: a short first line under 50 characters, a blank line, then the description
- Pro Git: Commit Guidelines: small commits and the imperative mood
- Tim Pope, A Note About Git Commit Messages (2008): the 50-character summary and 72-character body
- Chris Beams, How to Write a Git Commit Message (2014): the seven rules, and the test “If applied, this commit will…”