How to write better Commit messages
In collaborative software development, commit messages serve as the narrative thread that weaves together the evolution of a codebase.
They provide context, rationale, and a historical record of changes, facilitating collaboration, debugging, and maintenance. Despite their importance, commit messages are often overlooked or hastily written. This guide delves into best practices for crafting meaningful commit messages that enhance the quality and maintainability of your projects.
Why Commit Messages Matter
Commit messages are more than just annotations; they are a critical component of version control systems, serving multiple purposes:
- Historical Record: They document the progression of a project, allowing developers to trace the evolution of features and fixes.
- Collaboration: Clear messages facilitate understanding among team members, especially in large or distributed teams.
- Debugging: Tools like
git bisect
rely on informative commit messages to identify regressions efficiently. - Automation: Structured messages can trigger automated processes such as changelog generation and semantic versioning.
Anatomy of a Great Commit Message
A well-structured commit message typically consists of three parts:
- Subject Line: A concise summary of the change, ideally 50 characters or fewer.
- Body (optional): A detailed explanation of the change, its purpose, and any relevant context, wrapped at 72 characters per line.
- Footer (optional): References to issues, tickets, or breaking changes.
Example:
feat: add user authentication module
Implemented JWT-based authentication to secure user endpoints.
This change introduces login and registration functionalities.
Closes #42
Best Practices for Writing Commit Messages
1. Use the Imperative Mood
Begin the subject line with a verb in the imperative mood, as if giving a command. This aligns with Git’s default message style and clearly states the change’s intent.
Examples:
- ✅ “Add search functionality”
- ❌ “Added search functionality”
- ❌ “Adding search functionality”
2. Keep the Subject Line Concise
Aim for 50 characters or fewer in the subject line. This ensures readability across interfaces and tools.
3. Separate Subject from Body with a Blank Line
Insert a blank line between the subject and the body to avoid formatting issues and improve legibility.
4. Wrap the Body at 72 Characters
For better readability, especially in terminal-based tools, wrap the body content at 72 characters per line.
5. Explain the What and Why, Not the How
Commit messages should describe what was changed and why. The “how” can typically be inferred from the diff.
6. Reference Relevant Issues or Tickets
Link to related issues or tickets in the footer to provide additional context.
Example:
Closes #123
Related to #456
7. Use Consistent Formatting
Adopting a standardized format across your team or project improves clarity and sets expectations.
Leveraging Conventional Commits
The Conventional Commits specification introduces a structured way to write commit messages, which helps automate versioning, changelog generation, and CI/CD processes.
Format:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
Common Types:
feat
: A new featurefix
: A bug fixdocs
: Documentation updatesstyle
: Code formatting changesrefactor
: Code changes that neither fix a bug nor add a featuretest
: Adding or fixing testschore
: Miscellaneous maintenance
Example:
fix(auth): resolve token expiration issue
Adjusted the token refresh logic to handle edge cases where tokens expired prematurely.
Closes #789
Common Pitfalls to Avoid
- Vague Messages: Avoid uninformative messages like “fix bug” or “update code.”
- Overly Long Subject Lines: Keep the summary short and to the point.
- Combining Multiple Changes: Avoid lumping unrelated changes into a single commit.
- Neglecting Context: Include relevant context in the body when necessary.
Conclusion
Clear, structured, and informative commit messages are essential to professional software development. They enhance collaboration, make history navigation easier, support automated tooling, and improve overall project quality. By following the principles in this guide, you contribute not just code, but clarity.
FAQs
Q1: Why should I use the imperative mood in commit messages?
A: It aligns with Git conventions and clearly states what the commit does.
Q2: Is it necessary to write a body for every commit message?
A: Not always, but it’s beneficial when the subject line doesn’t capture all the
context.
Q3: How do Conventional Commits help in project management?
A: They standardize messages, making it easier to automate changelogs and manage
releases.
Q4: Can I reference multiple issues in a single commit message?
A: Yes, you can list multiple issues in the footer.
Q5: Should I include implementation details in the commit message?
A: Focus on the purpose and impact; code reviews and diffs show the
implementation.
Check viewARU - Brand Newsletter!
Newsletter to DEVs by DEVs — boost your Personal Brand & career! 🚀