How to write an effective Git commit message?

Updated

DevOps
Git
Collaboration
Software Development
Productivity
A conceptual drawing illustrates a developer thinking about how to write a good Git commit message.

An effective Git commit message is a concise explanation of the changes done in the source code. A commit message has a structure like an email subject and body. First, think about why we need an effective Git commit message?

  • Git commit messages improves team productivity. While in most situations, developers are working in a team. So other team members need clear information about the source code changes. That's why the Git commit message needs to be clear and understandable for source code reviewers.

  • You can easily track issues and bugs with Git commit messages. When a critical issue happens in a production environment, we need to fix the issue as soon as possible because it will affect the organization's reputation and brand name. Hence, our Git commit message should be clearer for other team members.

  • Better Git commit messages allows us to create better internal release notes. After implementing a new feature in a project, the developer needs to create and share internal release notes with the QA team. In release notes, the developer needs to mention bug fixes, issues, and functionalities. This is how the Git commit messages play a vital role in internal release notes.

  • Proper Git messages allows new developers to easily adapt the flow of source code. These days people rapidly switch their job from one organization to another organization. When a new developer joins an organization, he needs to understand the source code flow and get started as soon as possible. A good Git commit message will smoothen the knowledge transfer process for the new developer.

  • Precise Git commit messages are very important in Open Source software development. If you want to contribute to an Open Source project, you need to write a clear commit message because project maintainers, contributors, and followers are from different countries. When you push a Git commit to the repository, your commit message is open to the world, and many developers refer to the commit message to understand your contribution in a better way.

How to write an effective subject and message body for Git commit

The subject of the Git commit should contain a short description that references the source code changes, and the message body of the Git commit must have an extensive description referencing the provided subject. An effective subject and message body for a Git commit must meet the following criteria.

  • The subject of the Git commit should begin with capitalized character.

  • Make sure subject of the Git commit is described in an imperative mood.

  • Try limiting the subject of the Git commit within 50 characters.


    # Correct:
    Add multi-factor authentication

    # Wrong:
    add multi-factor authentication
    Adding multi-factor authentication
    
  • The message body of a Git commit is an optional thing.

  • For better readability in Linux/Unix terminals, try wrapping the message body of the Git commit by 70 characters/line.

  • It is good to write the message body of Git commit concisely. If possible, use a hyphen or asterisk symbol to represent bullet points in the message body of the Git commit.

  • Always add an issue tracker identifier at the bottom of the message body of the Git commit.


    # Example:
    - Implemented QR code based OTP generator.
    - Integrated with main authentication module.
    - Allowed enable/disable multi-factor authentication feature.

    Issue Tracker: #7343
    
  • Always use a blank line to separate the subject of the Git commit from the message body.

Here is the combined view of the subject and message body of a Git commit. If you have any queries, please ask the SloopStash community to find an answer.


    Add multi-factor authentication

    - Implemented QR code based OTP generator.
    - Integrated with main authentication module.
    - Allowed enable/disable multi-factor authentication feature.

    Issue Tracker: #7343
    

Hope it helps. Thank you.