Good commit messages are essential for a healthy codebase. They provide context, aid in collaboration, and make the history of a project much easier to follow. When you’re revisiting code after months (or even years), well-structured commit messages can help you understand what was changed and why.
To write an effective commit message, follow these practices, as recommended by Chris Beams:
- Separate the subject from the body: Your subject line should be clear and concise, but if you need to provide more detail, separate the subject from the body with a blank line.
- Limit the subject line to 50 characters: A short subject line ensures that it is easy to read and doesn’t get cut off in most interfaces.
- Capitalize the subject line: Start your subject line with a capital letter to maintain consistency and readability.
- Avoid periods at the end of the subject line: Since the subject is a brief summary, there’s no need to use a period.
- Use the imperative mood in the subject line: This makes it easier to read and conveys a sense of action. For example, write “Add feature” instead of “Added feature” or “Adding feature.”
- Wrap the body text at 72 characters: This ensures that your message remains readable across different interfaces and tools.
- Explain what and why in the body: The body of your commit message should focus on what was changed and why the change was made, rather than how it was done.
By following these simple rules, you’ll be contributing to a cleaner, more manageable codebase. As Chris Beams says, these practices are about hygiene, and good hygiene always pays off in the long run.