Conventional Commits Pattern
Matheus Bernardes Spilari
Posted on August 26, 2024
Intro
In the world of software development, maintaining a clear and organized commit history is crucial for collaboration and project maintenance. The Conventional Commits Pattern emerged as a way to standardize commit messages, making it easier to read, automate processes, and generate software versions. In this article, we will explore what Conventional Commits are, how to use them, and why they are so important.
What Are Conventional Commits?
Conventional Commits is a specification that defines a set of rules for creating a more readable and structured commit history. It follows a format that consists of a type, an optional scope, and a brief description of the change.
Structure of a Commit Message
A commit message following the Conventional Commits standard adheres to the structure below:
<type>[optional scope]: <brief description>
[optional body]
[optional footer]
- Type: Indicates the category of the change in the code (e.g., feat, fix, docs, style, etc.).
- Scope (optional): Defines the area of the code that was changed (e.g., core, api, ui).
- Brief description: A short phrase summarizing the change.
- Body (optional): Provides a more detailed description of the change if necessary.
- Footer (optional): Used to add additional information like "BREAKING CHANGE" or referencing issues.
Common Types in Conventional Commits
The most common commit types include:
- feat: A new feature for the user.
- 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: Code changes that neither fix bugs nor add features.
- test: Adding or modifying tests.
- chore: Updates to build tasks or configuration files.
- build: Updates the build process or external dependencies.
- perf: Indicates a change that improve the performance of the system.
- ci: Updates the CI(Continuous Integration) files.
- revert: Indicates the rollback of a previous commit..
Understanding Scope
Scope specifies the part of the code affected by the change, such as a module, component, or specific functionality. It allows developers to quickly understand where a particular change was made without needing to read the full commit description or the code itself.
For example, if you’re working on an application with different modules like auth, database, and ui, you can use scope to indicate which of these modules the change was made in. This is particularly useful in large projects where multiple areas can be modified simultaneously.
When to use Scope?
While using scope is optional, it is highly recommended in medium to large projects or when the codebase is divided into distinct modules or functionalities. Consistently using scopes helps keep the commit history organized and makes it easier to navigate commits when investigating bugs or performing reviews.
For example, if you’re reviewing the commit history and see a fix(auth): commit
, you immediately know the commit deals with a fix in the authentication functionality without needing a deeper investigation.
Crafting the Brief Description
The Brief Description in a Conventional Commit is a critical component that provides a concise summary of the change. It follows the commit type (and optional scope) after a colon and should be clear, direct, and informative. This section will explore how to write an effective brief description and why it is so important.
What is the Brief Description?
The Brief Description is a short, one-line summary that succinctly explains the change made in the commit. It should capture the essence of the modification, providing enough detail to understand what was done without being overly verbose. This summary is often the first thing a developer sees when reviewing commit logs, so it plays a significant role in conveying the nature of the changes.
Best Practices for Writing a Brief Description
-
Be Clear and Concise: The description should be straightforward and easy to understand. Avoid unnecessary jargon or overly technical language that might confuse other contributors.
- Good Example: fix(auth): correct password validation logic
- Bad Example: fix(auth): fixed stuff
-
Use the Imperative Mood: It is common practice to write the brief description in the imperative mood, as if you are commanding the code to perform an action. This style aligns with how commit messages are generally interpreted (e.g., "make this change" or "add this feature").
- Example: feat(ui): add dark mode toggle
-
Limit Length: Keep the description within 50 characters if possible. This ensures that the message remains readable in various interfaces, such as terminal views or commit history logs.
- Example: refactor(core): optimize data processing loop
-
Focus on What, Not How: The brief description should focus on what the change does rather than how it was done. The "how" can be elaborated on in the body of the commit message if needed.
- Good Example: docs(readme): update installation instructions
- Bad Example: docs(readme): changed wording in installation section
Benefits of Using Conventional Commits
- Clarity: Commit messages become more descriptive and standardized, making them easier to read and understand.
- Automation: Tools like semantic-release can be used to automate version generation based on commit types.
- Collaboration: A well-structured commit history makes it easier for other developers to review and contribute to the project.
Conclusion
Adopting the Conventional Commits Pattern is an important step to improve the quality of your project and facilitate collaboration. With clear standardization and a well-organized commit history, your development team can work more efficiently and productively.
📍 Reference
👋 Talk to me
Posted on August 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.