DEV Community

Cover image for The Art of Git Commits: Writing Messages That Your Future Self Will Thank You For
Sivantha Paranavithana
Sivantha Paranavithana

Posted on

The Art of Git Commits: Writing Messages That Your Future Self Will Thank You For

TL;DR

Learn how to write meaningful Git commits that enhance collaboration, make code reviews easier, and help maintain a clean project history. We'll cover commit message structure, conventional commits, and tools to enforce commit standards.

Introduction

If you're reading this, you probably know that Git is the most widely used version control system. While most developers use Git daily, not everyone makes the most of its commit functionality. Let's dive into how we can level up our commit game.

What is a Git Commit?

Think of a Git commit as a snapshot of your project at a specific point in time. It's like saving your progress in a video game - you want to save after completing important missions, not randomly in the middle of one.

Each commit represents:

  • A checkpoint in your project's timeline
  • A record of what changed and why
  • A reference point you can return to if needed

The Anatomy of a Good Commit

Have you ever looked at your commit history and wondered, "What was I thinking?" You're not alone. A good commit message should tell a story about what changed and why.

Bad Commit Messages:

fix stuff
updated code
final commit
final final commit
PLEASE WORK
Enter fullscreen mode Exit fullscreen mode

Good Commit Messages:

fix(auth): resolve JWT token expiration issue
feat(dashboard): add real-time data updates
docs: update API authentication guidelines
Enter fullscreen mode Exit fullscreen mode

Quick Tips for Better Commit Messages

Here are some immediate improvements you can make to your commit messages:

  1. Avoid Unnecessary Capitalization
    Instead of "Added New Feature", use "add new feature"

  2. Double Check Spelling
    Typos in commit messages make it harder to search history and look unprofessional

  3. Skip Punctuation in Summary Lines
    Don't end your commit message summaries with periods or other punctuation

These simple guidelines make your repository more visually appealing and easier to search through.

Use the Imperative Mood

A powerful trick for writing better commit messages is using the imperative verb form. Think of your commit message as completing this sentence:

"If applied, this commit will..."

Examples:

❌ "fixed login bug"
✅ "fix login bug"

❌ "updated user documentation"
✅ "update user documentation"

Real-world example:

add new Kief-the-kraken Keanu-Kief to keif gallery

modify menu.yml and keif-gallery.md related to ticket #12345
Enter fullscreen mode Exit fullscreen mode

Commit Frequently

One of the best ways to improve your commit messages is simply to commit more often. Here's why:

  • Smaller changes are easier to describe
  • Each commit has a clear, single purpose
  • You're less likely to lose work
  • Code reviews become more manageable

Imagine trying to write a commit message after a full day of uncommunicated changes - it's nearly impossible to be concise and clear. Instead, commit each meaningful change as you make it.

Git Commit Workflow

The journey to a perfect commit involves three main steps:

  1. Make Changes: Write your code and test it thoroughly
  2. Stage Changes: Use git add to select which changes to include
  3. Commit Changes: Create your snapshot with a meaningful message

Conventional Commits: A Standard That Makes Sense

Conventional Commits provide a structured format that makes commit histories readable and automated tools possible. This specification has become the de facto standard for many open-source projects and development teams.

One of the most powerful aspects of Conventional Commits is its ability to automate various DevOps processes:

  • Automated Changelog Generation: Tools can parse commit messages to automatically create and update changelogs
  • Version Management: Following Semantic Versioning (SemVer), commit types directly correlate to version bumps:
    • fix: commits trigger PATCH version increments (1.0.0 -> 1.0.1)
    • feat: commits trigger MINOR version increments (1.0.0 -> 1.1.0)
    • BREAKING CHANGE: commits trigger MAJOR version increments (1.0.0 -> 2.0.0)
  • Release Management: Automated release notes and version tagging based on commit history
  • CI/CD Integration: Automated deployment workflows based on commit types and scopes Here's the basic structure:
<type>[optional scope]: <description>

[optional body]

[optional footer(s)]
Enter fullscreen mode Exit fullscreen mode

Common Types:

  • feat: New features
  • fix: Bug fixes
  • docs: Documentation changes
  • style: Formatting changes
  • refactor: Code restructuring
  • test: Adding or modifying tests
  • chore: Maintenance tasks

Real-world Example:

feat(payment): implement PayPal integration

Add PayPal as a payment option for checkout process.
This change includes:
- PayPal SDK integration
- Payment verification flow
- Success/failure handling

BREAKING CHANGE: Updates payment processor interface
Closes #123
Enter fullscreen mode Exit fullscreen mode

Git Hooks: Your Commit Quality Guardian

Git hooks are scripts that run automatically before or after Git commands. They're perfect for enforcing commit message standards.
We can run Commitlint as a commit hook to ensure the quality of our commit messages.

Setting Up Commitlint

  1. Install commitlint:
npm install -g @commitlint/cli @commitlint/config-conventional
Enter fullscreen mode Exit fullscreen mode
  1. Add configuration:
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
Enter fullscreen mode Exit fullscreen mode
  1. Install husky for Git hooks:
npm install husky --save-dev
npx husky install
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1'
Enter fullscreen mode Exit fullscreen mode

You can refer to Commitlint Documentation for more details.

Tools to Make Your Life Easier

If you're using VS Code, the "Conventional Commits" extension by vivaxy is a game-changer. It provides:

  • Interactive commit message building
  • Type and scope suggestions
  • Conventional Commits specification compliance

Install the extension here

The Long-Term Benefits

Investing time in writing quality commit messages pays off in numerous ways:

  • Easier debugging when issues arise
  • Smoother code reviews
  • Better project documentation
  • Clearer project history
  • More efficient team collaboration

Remember: The time you spend writing good commit messages is an investment in your project's future maintainability.

Conclusion

Writing good commit messages is an art that pays dividends in the long run. It makes collaboration smoother, code reviews more efficient, and debugging less painful. Start implementing these practices today, and your future self (and teammates) will thank you.

Discussion

What's your approach to writing commit messages? Share your best practices and tools in the comments below!


If you found this article helpful, consider following me for more development tips and best practices.

Top comments (0)