Git & GitHub ยท Chapter 8 of 42

Writing Good Commit Messages

A commit message explains what changed and why. Good commit messages make it much easier for you and your teammates to understand project history months or years later.

A common convention is a short summary line under 50 characters, written in the imperative mood (e.g., "Fix bug" not "Fixed bug"), followed by an optional blank line and a more detailed body if needed.

Syntax
git commit -m "Short summary"
git commit

Writing a good summary

Keep the first line short and specific, such as "Fix null pointer in login form" rather than vague messages like "fix stuff" or "updates".

Adding a detailed body

For complex changes, run `git commit` without -m to open your editor, write a summary line, leave a blank line, then explain the reasoning in more detail below.

Example 1 (bash)
git commit -m "Fix crash when cart is empty"
Output
[main 9c2e1aa] Fix crash when cart is empty

A clear, specific summary describes exactly what the commit fixes.

Example 2 (bash)
git commit -m "Refactor auth module" -m "Splits login and signup logic into separate files for clarity."
Output
[main 4d1f0bb] Refactor auth module

Using two -m flags creates a summary line and a longer body message.

Key points

  • Good commit messages explain what changed and why.
  • Keep the summary line short and use the imperative mood.
  • Avoid vague messages like 'fix stuff' or 'updates'.
  • A blank line separates the summary from a detailed body.
๐Ÿ’ก Note: Future you (and your teammates) will thank you for writing clear commit messages when debugging old code.

๐Ÿ“ Quick Quiz

1. What tense should a commit summary use?

2. Which is a good commit message?

3. How can you add a detailed commit body?