In this lesson, you will learn how to craft professional commit messages that communicate the "why" behind your code changes using the imperative mood.
The Imperative Mood
The industry standard for commit messages is to use the imperative mood—writing as if you are giving a command to the codebase. Think of your commit message as the instruction that, when applied, results in the changes you’ve made.
Instead of writing "Fixed the bug," write "Fix the bug." Instead of "Added authentication," write "Add authentication." This creates a clean, uniform project history that reads like a set of build instructions.
Structuring for Clarity
A professional commit message should follow a strict structure:
- The Summary: A concise, 50-character limit summary written in the imperative mood.
- The Blank Line: Separates the summary from the details.
- The Body: An explanation of the why (not the how). Use this space to describe the problem your change solves or the logic behind a complex refactor.
Example:
Refactor user authentication service
The previous implementation failed when handling concurrent token
refresh requests. This change introduces a mutex lock to ensure
that only one refresh operation occurs at a time, preventing
inconsistent session states for the user.
Why This Matters
As you progress into advanced workflows like interactive rebasing or squashing, your commit messages serve as the primary documentation for the project's evolution. When you return to a complex feature six months later, these messages allow you—and your teammates—to reconstruct the intent of every modification without having to parse every line of code.
Avoid messages like "Fixed stuff" or "More changes," which force collaborators to dig into the diff to understand your work. By focusing on the why and using the imperative mood, you turn your project history into a reliable, professional map of your development journey.
Key Takeaway: Always use the imperative mood and provide context for your changes to transform your commit history from a list of edits into a clear, searchable project narrative.