First steps · lesson 7 of 8
Commands and ideas in this lesson
AOne changeone commit does one thing
BSubject linea short command: Add, Fix, Move
CBodywhy, after a blank line
Dgit commit -m -msubject and body from the command line

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:

  1. A subject line of about 50 characters or less, so it fits in git log --oneline and on hosting sites.
  2. Start with a capital letter, and no full stop at the end.
  3. 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…".
  4. 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:

Terminal
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

  1. Add pancit bihon recipe

  2. Cover the leche flan with foil while steaming

  3. fixed stuff

  4. Update adobo.md

  5. Added the sinigang recipe.

  6. add halo halo

  7. Change the salt in the adobo from a little salt to one teaspoon because Lola measured it

  8. wip

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.

  1. Ana wrote: changes

    What the commit really did: Added Lola Remy’s sinigang recipe as sinigang.md.

    0/50
  2. Ana 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/50
  3. Ana wrote: update

    What the commit really did: Moved the README’s instructions for building the index page to the top.

    0/50

Spot the trap: "fixed stuff"

Spot the trapLola Remy

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

Which subject line follows the rules?
What should a commit message’s body explain?
Ana fixed a typo in the sinigang recipe and added a new halo-halo recipe. How many commits?

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.

All sources for Simple Git