How to Write Good Commit Messages in Git: Best Practices
A good
commit message starts with a short, clear summary line under 50 characters, followed by a blank line and a detailed explanation if needed. Use the imperative mood (like giving commands) and keep messages focused on what and why, not how.Syntax
A good commit message has two parts:
- Subject line: A short summary (max 50 characters) in imperative mood.
- Body (optional): A detailed explanation wrapped at 72 characters, separated by a blank line.
This format helps others quickly understand the change and its purpose.
bash
git commit -m "Subject line" # or with body: git commit -m "Subject line" -m "Detailed explanation of the change, why it was made, and any important notes."
Example
This example shows a commit message fixing a bug in the login feature with a clear subject and detailed body.
bash
git commit -m "Fix login error on invalid password" -m "The login form now correctly shows an error message when the password is invalid. This fixes issue #123 where users saw a blank screen instead."
Output
On branch main
Your branch is up to date with 'origin/main'.
[main abc1234] Fix login error on invalid password
1 file changed, 5 insertions(+), 2 deletions(-)
Common Pitfalls
- Writing vague messages like "fix stuff" or "update code" that don't explain the change.
- Using past tense instead of imperative mood (e.g., "Fixed bug" instead of "Fix bug").
- Making the subject line too long or combining multiple unrelated changes in one commit.
- Skipping the blank line between subject and body, which breaks readability.
bash
Wrong: git commit -m "Fixed bug in login" Right: git commit -m "Fix bug in login" -m "Correct the password validation to prevent blank screen on failure."
Quick Reference
Keep these tips in mind for every commit message:
- Use imperative mood: "Add", "Fix", "Update"
- Limit subject line to 50 characters
- Separate subject and body with a blank line
- Wrap body text at 72 characters
- Explain why the change was made, not how
Key Takeaways
Start commit messages with a short, clear subject line in imperative mood.
Separate the subject and body with a blank line for readability.
Use the body to explain why the change was made, wrapped at 72 characters.
Avoid vague or overly long messages and combine related changes only.
Consistent commit messages help your team understand project history easily.