Mastering the Art of Commit Messages with Conventional Commits

akash javaliakash javali
4 min read

In the world of software development, clear and concise commit messages are crucial. They provide insight into the history of a project, making it easier to track changes, debug issues, and understand the evolution of the codebase. However, writing meaningful commit messages can sometimes be overlooked, leading to a messy and confusing commit history. This is where the Conventional Commits specification comes into play.

What are Conventional Commits?

Conventional Commits is a standardized convention for commit messages. It provides a set of rules for creating an explicit commit history, which makes it easier to write automated tools on top of. The primary goal is to make it clear what kind of changes are included in each commit, whether it's a new feature, a bug fix, a documentation update, or something else.

The conventional commit message format looks like this:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Let’s break down these components:

  1. Type: This is a mandatory field that describes the nature of the change. Common types include:

    • feat: A new feature

    • fix: A bug fix

    • docs: Documentation changes

    • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)

    • refactor: A code change that neither fixes a bug nor adds a feature

    • perf: A code change that improves performance

    • test: Adding missing tests or correcting existing tests

    • chore: Changes to the build process or auxiliary tools and libraries such as documentation generation

  2. Scope: This is an optional field that specifies the area of the codebase the change affects, like a specific module or component.

  3. Description: A brief summary of the change in the imperative mood, not capitalized, and without a period at the end. For example, “add new login feature”.

  4. Body: This optional section provides additional contextual information about the change. It can include the motivation for the change, its impact, and any additional details.

  5. Footers: These are optional and can include references to issues or breaking changes. For example, BREAKING CHANGE: update API endpoint for user data.

Why Use Conventional Commits?

  1. Clarity and Consistency: By adhering to a common structure, developers can quickly understand the nature of a commit without needing to decipher ambiguous or incomplete messages.

  2. Automation: Conventional Commits enable automation of release notes generation, versioning, and changelog creation. Tools like Semantic Release can parse commit messages to determine the next version number and generate release notes automatically.

  3. Improved Collaboration: Clear commit messages make it easier for team members to understand what has been done, why it was done, and how it fits into the larger project.

  4. Better History Tracking: With a structured commit history, it becomes easier to track down when and why changes were made, aiding in debugging and auditing.

Examples of Conventional Commits

Let’s look at some examples to illustrate how to write conventional commits:

  • Adding a new feature:

      feat(auth): add OAuth2 login functionality
    
  • Fixing a bug:

      fix(profile): correct the display of user bio on the profile page
    
  • Updating documentation:

      docs(readme): update installation instructions
    
  • Refactoring code:

      refactor(auth): simplify token validation logic
    
  • Performance improvement:

      perf(database): optimize query for fetching user data
    
  • Adding tests:

      test(auth): add unit tests for OAuth2 login
    
  • General maintenance tasks:

      chore(deps): update dependency lodash to v4.17.21
    

Getting Started with Conventional Commits

To start using Conventional Commits, encourage your team to follow the specification. Here are some practical steps:

  1. Educate Your Team: Share the Conventional Commits specification with your team and explain the benefits.

  2. Adopt Tools: Use tools that support Conventional Commits, like commitizen for crafting commit messages and semantic-release for automating releases.

  3. Integrate Checks: Integrate commit linting tools like commitlint into your CI/CD pipeline to enforce the commit message guidelines.

  4. Lead by Example: As a team leader or a senior developer, set an example by consistently using Conventional Commits in your own work.

By adopting Conventional Commits, you can ensure a more manageable and understandable commit history, paving the way for better collaboration, easier maintenance, and smoother project evolution. Happy committing!

0
Subscribe to my newsletter

Read articles from akash javali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

akash javali
akash javali

A passionate 'Web Developer' with a Master's degree in Electronics and Communication Engineering who chose passion as a career. I like to keep it simple. My goals are to focus on typography, and content and convey the message that you want to send. Well-organized person, problem solver, & currently a 'Senior Software Engineer' at an IT firm for the past few years. I enjoy traveling, watching TV series & movies, hitting the gym, or online gaming.