← Back to Git Course | Chapter 8: Best Practices | Lesson 2 of 6

Git Commit Message Best Practices

The Structure of a Good Commit Message

The convention is a short summary line (ideally under ~50 characters), a blank line, then a body paragraph explaining the reasoning behind the change. The summary alone should make sense in a one-line git log --oneline list; the body is where you explain *why*, since the diff itself already shows *what*.

Example: The Structure of a Good Commit Message

bash
git commit -m "Fix null pointer in checkout flow" -m "The cart total crashed when the discount field was empty because it was never defaulted to zero."

Using Semantic Prefixes

Prefixes like feat:, fix:, docs:, and style: (part of the Conventional Commits convention) let you tell at a glance what kind of change a commit is without reading its full message, and tools like changelog generators can parse them automatically to build release notes.

Example: Using Semantic Prefixes

bash
git commit -m "feat: add dark mode toggle"
git commit -m "fix: correct off-by-one error in pagination"

Keeping Messages Imperative

Writing "Add login validation" instead of "Added login validation" matches the phrasing Git's own auto-generated messages use (e.g. "Merge branch"), and reads naturally as a command describing what applying this commit *does* — a convention that keeps a mixed-author log style-consistent.

Example: Keeping Messages Imperative

bash
git commit -m "Add login validation"

Referencing Issues and Pull Requests

Writing Closes #15 (or Fixes, Resolves) in a commit or PR description isn't just documentation — GitHub, GitLab, and similar platforms parse those exact keywords and automatically close the referenced issue the moment the commit merges.

Example: Referencing Issues and Pull Requests

bash
git commit -m "Fix broken image upload" -m "Closes #15"

Amending a Commit Message

git commit --amend opens your last commit's message in the editor for you to fix, replacing the commit entirely rather than adding a new one — but only do this before pushing, since amending a pushed commit changes its hash and requires a force push to sync.

Example: Amending a Commit Message

bash
git commit --amend -m "Fix typo in login form label"
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.