Skip to content

Instantly share code, notes, and snippets.

@mohit-raj-purohit
Last active July 16, 2024 03:48
Show Gist options
  • Save mohit-raj-purohit/1b59c879f26d7f6b6546753d32a9ebf1 to your computer and use it in GitHub Desktop.
Save mohit-raj-purohit/1b59c879f26d7f6b6546753d32a9ebf1 to your computer and use it in GitHub Desktop.
How to create a git message template for better commit messages

Using Git Commit Message Templates to Write Better Commit Messages

Introduction

In this document, we will discuss how to set up and use Git commit message templates to write better commit messages.

Prerequisites

  • Git installed on your local machine
  • Basic understanding of Git and commit messages

Creating a Git Commit Message Template

Git allows you to define a commit message template that will be pre-populated whenever you create a new commit. To create a commit message template, follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to your Git repository using the cd command.
  3. Create a new file called .gitmessage in the root directory of your repository using a text editor or the command line. This file will serve as your commit message template.
  4. Open the .gitmessage file and define the structure of your commit message template. You can include placeholders for various parts of the commit message, such as the subject, body, and footer. Here's an example template:
Subject:
<type>(<scope>): <description> (max 50 characters)
Body:
<insert detailed description of changes made in the commit> (wrap at 72 characters)
Footer:
<insert any additional information, such as references or issue numbers>

You can customize the template to suit your team's needs, but it's generally recommended to include a subject, body, and footer in your commit messages for better clarity.
Save and close the .gitmessage file.

Configuring Git to Use the Commit Message Template

Once you have created your commit message template, you need to configure Git to use it by default. Follow these steps:

  1. Open your terminal or command prompt.

  2. Navigate to your Git repository using the cd command.

  3. Run the following command to set the path to your commit message template.

    git config --local commit.template .gitmessage

    This sets the commit.template configuration to the path of your .gitmessage file.

  4. Verify that the commit.template configuration has been set correctly by running the following command:

    git config --local --get-all commit.template

    You should see the path to your .gitmessage file printed in the output.

Writing Commit Messages Using the Template

Now that you have configured Git to use your commit message template, you can start writing commit messages using the template. When you create a new commit, Git will automatically open your commit message template in your default text editor with the predefined structure.

To write a commit message using the template, follow these steps:

  1. Run the following command to create a new commit:

    git commit  

  2. Your text editor will open with the commit message template pre-populated. Fill in the subject, body, and footer sections with relevant information about the changes made in the commit.

  3. Save and close the commit message template.

  4. Git will create the commit with the commit message you wrote using the template.

Conclusion

Using Git commit message templates can help you write better commit messages by providing a consistent structure and format. This makes it easier for team members to understand the changes made in a commit and helps with code review, debugging, and troubleshooting. By following the steps outlined in this document, you can set up and use Git commit message templates in your development workflow to improve the quality of your commit messages. Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment