🧠 Mastering Git: Writing Meaningful Commit Messages
In this article, we’ll explore why good commit messages matter and how you can follow best practices, including the popular conventional commit style (e.g., feat:
, fix:
).
Why Good Commit Messages Matter
Section titled “Why Good Commit Messages Matter”A well-written commit message:
- Explains why something was changed
- Helps your team understand the context behind changes
- Makes it easier to debug, review, or revert code
- Improves collaboration and code maintainability
- Can be used to auto-generate changelogs
Think of each commit as a log entry in the history of your project — not just for others, but also for your future self!
The Anatomy of a Good Commit Message
Section titled “The Anatomy of a Good Commit Message”A good commit message should be concise, descriptive, and follow a consistent format.
✅ Basic Format
Section titled “✅ Basic Format”<type>: <subject>
<body> (optional)
🔧 Type (required)
Section titled “🔧 Type (required)”The type describes what kind of change this is. These are typically short codes that help categorize the nature of the commit.
Here are some commonly used types:
Type | Description |
---|---|
feat | A new feature |
fix | A bug fix |
docs | Documentation only changes |
style | Code style changes (no logic) |
refactor | Code refactoring (no features/bugs) |
perf | Performance improvements |
test | Adding tests or fixing them |
chore | Maintenance tasks (e.g., build, deps) |
ci | CI/CD configuration changes |
build | Build system or external dependency changes |
revert | Reverts a previous commit |
📝 Subject (required)
Section titled “📝 Subject (required)”A brief summary (50 characters or less) of the change.
💬 Body (optional)
Section titled “💬 Body (optional)”If needed, explain what and why you did something. Avoid the “how” — the code shows that.
✨ Examples of Good Commit Messages
Section titled “✨ Examples of Good Commit Messages”feat: add user profile page
fix: prevent null pointer exception in login flow
docs: update contributing guide with setup instructions
refactor: extract validation logic into separate module
perf: optimize image loading speed by lazy-loading thumbnails
test: add unit tests for cart calculation logic
chore: upgrade dependencies in package.json
revert: revert accidental deletion of core library
🛠 Tips for Better Commits
Section titled “🛠 Tips for Better Commits”-
Use the imperative mood: Like “Fix bug” instead of “Fixed bug” or “Fixes bug”.
Example:feat: add password strength meter
✅ vsfeat: added password strength meter
❌ -
Keep it concise: Aim for clarity over verbosity.
-
Link to issues or PRs: If applicable, reference an issue or pull request.
Terminal window feat(auth): implement email verification #1234 -
Group related changes: Each commit should do one thing. Avoid large commits with unrelated changes.
-
Review before committing: Double-check your changes (
git diff
) and ensure your message reflects them accurately. -
Amend when necessary: Don’t be afraid to edit your last commit:
Terminal window git commit --amend
🧰 Tools That Help
Section titled “🧰 Tools That Help”- Commitizen – Interactive CLI to guide you through creating conventional commits.
- Husky + Commitlint – Enforce commit conventions via linting rules.
- VSCode Extensions – Many extensions help format and validate commit messages.
🎯 Summary
Section titled “🎯 Summary”Writing good Git commit messages is a small investment that pays off over time. By adopting a standard like Conventional Commits, you’ll make your repository more readable, searchable, and maintainable.
Start today with:
git commit -m "feat(blog): introduce conventional commit style"
📚 Further Reading
Section titled “📚 Further Reading”- Conventional Commits Specification
- How to Write Good Commit Messages (GitHub Blog)
- Tim Pope on Git Commit