Mastering commit messages: A guide to Conventional Commits and best practices

Published June 03, 2024. 6 min read

author image

Jagadeesh Sali, DevOps Engineer, EnLume

Effective communication underpins the success of any software development project. This principle extends far beyond well-written code comments and comprehensive documentation. Clear and concise commit messages play a crucial role in fostering collaboration, streamlining workflows, and ensuring the long-term maintainability of a codebase.

This blog post delves into the concept of Conventional Commits, a standardized approach to crafting commit messages. We'll explore the benefits of adopting this convention and equip you with best practices for writing informative and standardized messages that empower your development team.

What are Conventional Commits?

Conventional Commits are a well-established specification that promotes a structured format for crafting commit messages within a codebase. This approach encourages developers to adhere to a consistent pattern, fostering clarity and streamlining collaboration.

Each commit message adheres to a defined structure, typically consisting of:

  • A concise summary: This opening line captures the essence of the change introduced in the commit.
  • An optional body: This section provides more detailed information about the changes, if necessary.
  • An optional footer: The footer can be used to include additional details, such as issue tracking references or breaking changes.

By following these predefined rules, Conventional Commits ensure consistency and clarity in commit messages across a project.

Why Conventional Commits?

img

There are several compelling reasons to adopt Conventional Commits within your software development workflow:

  • Semantic versioning: By adhering to a standardized commit message format, developers can automate the process of determining version bumps based on the types of changes introduced.
  • Automated release notes: Conventional Commit messages provide the necessary context to generate release notes automatically, saving time and effort for developers and maintainers.
  • Enhanced collaboration: Consistent commit messages make it easier for team members to understand the history of a codebase, fostering collaboration and knowledge sharing.

Conventional Commits and semantic release

Conventional Commits play a crucial role in enabling semantic release. By adhering to a standardized commit message format, semantic versioning can be automated based on the types of changes introduced in each commit. This ensures that version numbers are incremented appropriately according to the significance of the changes. For instance:

  • A commit message prefixed with feat indicates the introduction of a new feature, which typically results in a minor version bump.
  • A commit message prefixed with fix denotes a bug fix, leading to a patch version increment.
  • Other prefixes like chore, docs, style, refactor, and test help categorize commits and determine their impact on versioning.

Semantic release tools analyze commit messages following the Conventional Commits format and automatically determine the next version number based on the highest level of change introduced since the last release. This automated process eliminates the need for manual versioning and ensures consistency and accuracy across releases.

General commit guidelines

img

    In addition to Conventional Commits, adhering to general commit guidelines enhances the quality and maintainability of a codebase:

    1. Tiny commits: Each commit should represent a single, focused change. Breaking down complex tasks into smaller, atomic commits makes it easier to understand the history of the codebase, isolate issues, and perform effective code reviews. Above all, it makes the reviewer's life easy.

    2. Compilable commits: Before committing changes, ensure that the code compiles successfully. Compiling code before committing helps maintain a functional state of the repository and prevents the introduction of broken code into the version control system.

    3. Meaningful commit messages: Write descriptive commit messages that clearly communicate the purpose and impact of the changes. A well-crafted commit message provides valuable context to collaborators and future maintainers, facilitating code comprehension and troubleshooting.

    4. Follow coding standards: Adhere to coding standards and best practices established by the project or organization. Consistency in coding style and formatting improves code readability and reduces friction during collaboration.

    5. Avoid mixing concerns: Each commit should focus on a single concern, whether it's implementing a feature, fixing a bug, or refactoring code. Mixing unrelated changes in the same commit makes it challenging to understand the purpose of the commit and can lead to unintended consequences.

    Other restrictions

    Beyond the guidelines mentioned above, consider imposing additional restrictions tailored to the specific needs of the project, such as:

    • Limiting Line Length: Enforce a maximum line length to prevent overly long lines of code, which can reduce readability and make code diffs harder to review.
    • Prohibiting Direct Commits to Main/Branch: Require changes to go through a pull request or merge request process, allowing for code review and ensuring that all changes are vetted before being merged into the main branch.
    • Requiring Descriptive Pull Request Titles and Descriptions: Encourage contributors to provide clear and informative titles and descriptions when submitting pull requests, facilitating effective code review and release management.

    List of conventional commit types along with their emojis

    This is a great way to visually distinguish different types of commits and make the commit history more engaging and understandable. Let's explore each type in more detail:

    test: Adding missing tests

    • This commit type is used for adding new tests or filling gaps in existing test coverage. Tests are crucial for ensuring code quality and preventing regressions, so adding missing tests is an essential part of maintaining a healthy codebase.

    feat: A new feature

    • Use this commit type when introducing new functionality or features to the codebase. Features enhance the capabilities of the software and provide value to users or stakeholders.

    fix: A bug fix

    • When addressing issues or bugs in the codebase, use this commit type. Bug fixes correct unexpected behavior or errors in the software, improving its reliability and stability.

    chore: Build process or auxiliary tool changes

    • Commits categorized as "chore" typically involve changes to the build process, configuration files, or other auxiliary tools. These changes are necessary for maintaining the development environment or supporting workflows but do not directly affect the functionality of the software.

    docs: Documentation only changes

    • Use this commit type for commits that solely involve updating documentation, such as README files, code comments, or developer guides. Clear and up-to-date documentation is essential for understanding and maintaining the codebase.

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

    • Refactor commits involve restructuring or improving the internal structure of the code without changing its external behavior. These changes aim to enhance code readability, maintainability, or performance without introducing new features or fixing bugs.

    style: Markup, white space, formatting, missing semicolons…

    • Style commits are related to cosmetic changes such as formatting, indentation, or whitespace adjustments. While these changes do not affect the functionality of the code, they contribute to consistent coding style and readability.
    • This commit type is used for changes related to Continuous Integration (CI) systems, such as configuration updates, pipeline changes, or integration with third-party services. CI plays a crucial role in automating testing and deployment workflows.

    perf: A code change that improves performance

    • Use this commit type for changes that optimize the performance of the codebase, such as reducing execution time, improving memory usage, or enhancing scalability. Performance improvements contribute to better user experience and resource efficiency.

    Conclusion

    Crafting clear and informative commit messages is essential for any software developer. Well-written messages enhance the understandability of a codebase and contribute to a smoother development process.

    Conventional Commits provide a standardized approach to structuring commit messages, offering numerous advantages. This convention streamlines workflows like automated releases and versioning, ensuring a more efficient development lifecycle. By adhering to best practices and embracing Conventional Commits, developers can foster better collaboration, elevate code quality, and simplify the overall software development experience.

    Incorporating Conventional Commits into your development workflow empowers you to write clear, concise, and informative commit messages that benefit your entire team. Start today and witness the transformative impact on your project's maintainability and collaboration.

    Reference: https://www.conventionalcommits.org/en/v1.0.0/