DEV Community

jangir-ritik
jangir-ritik

Posted on

A guide on writing clear and meaningful Commit messages: And how I write them

Effective communication and maintaining common ground are paramount in the development process of a software project. Collaborative coding thrives on seamless teamwork, regardless of team size or expertise levels, necessitating reliable checkpoints. One such crucial checkpoint is the commit—a vital component that enables developers to retrace their journey throughout the software development process. As the astute quote wisely states, "A core component of making great decisions is understanding the rationale behind previous decisions." This insightful perspective underscores the importance of comprehending past decisions to avoid pitfalls and enhance future outcomes.

Now, imagine being thrust into a situation where you're tasked with working on a codebase left behind by someone who neglected to write meaningful commits or comments. This individual abruptly departed, leaving an incomplete code that holds a wealth of information but lacks organization and clarity. The mere thought of it sends a shiver down your spine, evoking frustration and the urgent need for a deep breath. If you've experienced this predicament, fear not. In this article, we will embark on a journey to positively influence your workflow by emphasizing the significance of commits and providing guidance on how to write them effectively.

By delving into the importance of high-quality commit messages, we'll explore how they enable easy referencing, debugging, and code reviews. We'll uncover the key elements of a well-crafted commit message, including a concise summary of the changes, optional detailed descriptions, and references to relevant issue trackers or tags. Through this comprehensive exploration, you will gain the knowledge and tools to navigate and understand complex codebases left behind by others with greater ease and efficiency.

So, whether you're a developer who has experienced the frustration of untangling code left without proper documentation or simply someone seeking to optimize their development practices, this article is tailor-made for you. Join us as we unravel the mysteries of commits and empower ourselves to write them effectively, creating a smoother and more productive collaboration within our teams.

Reasons to write better commits

Writing clear and descriptive commits is an essential practice in software development. Well-crafted commits benefit your future self and contribute to effective collaboration within your team. Here are several reasons why investing time and effort into writing better commits is worth it:

1. Easy references/debugging/reviews in the future
Clear commit messages enable you or others to quickly understand the purpose and context of changes made to the codebase. When debugging issues or reviewing code, well-documented commits act as a valuable reference, saving time and reducing frustration. For example:

  • poor commit message

    Commit: "Fix bug"
    Description: "Code changed"
    
  • improved commit message

    Commit: "Fix issue with user login validation"
    Description: "Updated the login form validation logic to properly handle empty username and password fields."
    

2. Improved team communication
Well-written commits enhance communication among team members. Colleagues can easily comprehend the changes made, who made them, and why they were made. This leads to better knowledge sharing, and streamlined collaboration. For example:

  • poor commit message

    Commit: "Feature update"
    Description: "Refactored code"
    
  • improved commit message

    Commit: "Feature update"
    Description: "Refactored code"
    

3. Easier review for management/stakeholders
Clear and concise commit messages make it simpler for managers and stakeholders to track progress and understand the impact of changes. When presenting updates or seeking approval, well-documented commits facilitate comprehension and decision-making.

  • poor commit message

    Commit: "Code changes"
    Description: "Work in progress"
    
  • improved commit message

    Commit: "Implement payment integration with PayPal"
    Description: "Integrated PayPal payment gateway to allow users to securely make purchases using their PayPal accounts."
    

4. Version control and branching strategies
Well-defined commits contribute to effective version control, particularly when following semantic versioning (semVer) practices. By clearly outlining the changes in each commit, it becomes easier to identify and manage versions of your software. Additionally, descriptive commits aid in determining the appropriate branching strategies to adopt. For example:

  • poor commit message

    Commit: "Update code"
    Description: "Added new functionality and fixed some bugs"
    
  • improved commit message

    Commit: "Prepare release v1.2.0"
    Description: "Added new feature X in preparation for the v1.2.0 release."
    

5. Better control over the codebase
Operations like fixing, dropping, or entire recourses while working on a feature branch or during the course of development becomes easy by maintaining high value commits. Small, well-organized commits are more manageable and provide flexibility. For example:

  • poor commit message

    Commit: "Stuff"
    Description: "More stuff"
    
  • improved commit message

    Commit: "Optimize database queries for improved response time"
    Description: "Refactored database query logic to utilize indexes, reducing average response time by 20%."
    
  • While often ignored, the author of a commit is as vital as the subject and body. After all, the name reveals who committed the changes so we know who to speak to for support if we want to talk about architecture/implementation or get clarity on changes in general. Ideally, a commit’s documentation should provide all of the context necessary so no one has to track down the original author in the first place — assuming the engineer is still part of the team/company! Sometimes this documentation will be the only explanation you ever get.

Best practices related to commit messages

To ensure clear and meaningful commit messages, consider the following best practices when working with version control systems:

1. Commit granularity
Create single-purpose commits that focus on specific tasks, features, or bug fixes. This maintains a clear history of changes and facilitates understanding and collaboration among team members.

2. Commit frequency
Commit your changes early and frequently to track progress and facilitate collaboration. Regular commits also enable easier rollback or merging of changes if needed.

3. Code quality checks
Run tests and build your code before committing to ensure its integrity and minimize the introduction of errors into the codebase.

4. Clear and concise commit messages
Use past tense and keep commit messages concise yet descriptive. Clearly convey the purpose of the commit and the changes made.

5. Imperative verb usage
Start commit messages with an imperative verb to provide a consistent and action-oriented tone. Use verbs like "Fix," "Add," "Update," or "Refactor" to clarify the nature of the commit.

6. Integrate with issue tracking
Link commits to relevant issues in your issue-tracking system using references. This allows for easy traceability and provides context for future reference. For example:

Commit: "FIX: in user registration (Issue #123)"
Enter fullscreen mode Exit fullscreen mode

7. Meaningful commit messages
Avoid generic commit messages like "fixed a bug" or "updated code." Instead, strive to provide specific and meaningful descriptions of the changes made.

Personal anecdote: The Essential Commit Message Prefixes

When it comes to writing effective Git commits, maintaining clarity and consistency is crucial. Throughout my experience, I've come to realize the value of using specific prefixes. These prefixes serve as a guide for crafting meaningful commit messages that are easy to understand and follow. By adhering to these prefixes, developers can enhance collaboration and streamline the process of reviewing and tracking changes. Let's explore the five essential prefixes that can significantly improve the quality of your commit messages.
1. Fixed
This prefix indicates that a commit addresses a bug or an issue in the codebase. It signifies that a problem has been resolved. For example:

FIX: issue with user login validation
Enter fullscreen mode Exit fullscreen mode

2. Removed
Use this prefix when a commit involves the removal of code, files, or any other resources. It highlights the elimination of unnecessary or deprecated elements. For instance:

REMOVE: unused dependency from project configuration
Enter fullscreen mode Exit fullscreen mode

3. Added
When introducing new features, functionalities, or components to the codebase, the prefix "Added" is an excellent choice. It helps in clearly identifying additions to the project. For example:

ADD: support for multi-factor authentication
Enter fullscreen mode Exit fullscreen mode

4. Updated
The prefix "Updated" is appropriate when a commit involves modifications or improvements to existing code or resources. It conveys that changes have been made to enhance or refine the system. For instance:

UPDATE: data validation logic for improved security
Enter fullscreen mode Exit fullscreen mode

5. Refactored
This prefix is suitable for commits that involve code refactoring or restructuring without changing the external behavior. It emphasizes the enhancement of code quality or architecture. For example:

REFACTOR: database access layer for improved performance
Enter fullscreen mode Exit fullscreen mode

When it comes to writing Git commits, I understand the need for a commit message format that allows for easy skimming through the commit history. In addition to the essential prefixes mentioned earlier, you can incorporate additional prefix tags such as "MINOR" and "MAJOR" to further enhance readability and provide quick insights into the nature of the changes.

For instance, you can use the "MINOR" tag to indicate commits that include minor enhancements, improvements, or small-scale changes:
MINOR: Update styling of login button

On the other hand, the "MAJOR" tag can be used to denote commits that introduce significant changes or major features to the codebase:
MAJOR: Implement new user authentication system

By incorporating these prefix tags alongside the essential prefixes like "Fixed," "Removed," "Added," "Updated," and "Refactored," you can create a commit message structure that facilitates skimming through the commit history and provides a quick overview of the changes made. This approach improves collaboration and helps team members comprehend the impact of each commit more efficiently.

Anatomy of a commit message

Image description
A commit message typically consists of three parts: a short summary of the changes, an optional longer description of the changes, and any issue tracker references or tags. The summary should be concise and descriptive, using imperative verbs. The longer description should provide context and explain the "why" of the changes. Finally, issue tracker references or tags can be used to link commits to specific issues.

  • A Git commit summary is always meant to tell us what is being committed to the code base and nothing more. Sometimes you might see people add reference IDs or other forms of identification but the subject is not the place for this information. Metadata, like that, should always go in the body of the commit message via trailers.

How others write commit messages!

  • Linus Torvalds (creator of Linux) uses short, to-the-point commit messages that describe the changes made in a few words.
  • The AngularJS team uses a specific format for their commit messages, including a brief summary, a longer description, and a footer with issue tracker references and tags.
  • The Git project itself has a set of guidelines for writing commit messages, including using the imperative mood, keeping the subject line under 50 characters, and providing a detailed explanation of the changes in the body of the message.

For more details, check out the Git Lint Commit Subject Prefix style guide.

Overall, the key is to find a style that works for you and your team, and to be consistent in your approach to writing commit messages.

Top comments (2)

Collapse
 
jack94507501 profile image
Jack

I really like this post. Thanks!

Collapse
 
pooja2574 profile image
pooja patel

Nicely described. It helps a lot to clear the confusion whether to write a proper meaningful comment or not. Comments really helps to identify the previous commit with proper objective defined. It is a good practice to maintain your code and commits.