Git

How to define a Git Commit Message Template?

While working on the project, committing is a crucial feature so that every developer can see who has contributed and made the changes. Sometimes, a single line commit message is not enough as it creates confusion and is a bit complex to understand for other developers about the changes.

Keeping that in mind, Git introduced the feature of defining the template for the commit message that permits you to handle such scenarios. You can define a commit message template which will appear on every user interface when committing.

This write-up will provide the instructions to define a Git commit message template.

How to Define a Git Commit Message Template?

To define a Git, commit message template, the “.gitmessage” file is utilized. You can define the commit message template inside this file and add it in your main directory. Let’s dive into the practical demonstration of doing this.

Step 1: Move to Project Directory

First, open the terminal, and move to your project directory:

cd git

Step 2: Create “.gitmessage” File

Afterward, create the “.gitmessage” file with the touch command:

touch .gitmessage

The file “.gitmessage” has been created.

Step 3: Define Commit Message Template

After creating the “.gitmessage” file, open it with the nano editor and define the commit message template. For instance, we have added the 3 portions, subject, body, and footer. You can define the template as per your needs and preferences:

Subject:

<Enter Subject with Short Description>

Body:

<Enter Detailed Description of the Changes You Have Made>

Footer:

<Enter Additional Information Like References>

Save the file and exit from the editor.

Step 4: Set Template to Default

For the default calling of this template in your main project, run this command in the terminal:

git config --local commit.template .gitmessage

The file “.gitmessage” is set to default for the commit message template.

Step 5: Check the Set Template File

To verify and check the template file is set to default or not, the following command is considered:

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

Step 6: Commit to Verify

Now, run the “git commit” command in the terminal and you will get the defined template in the editor:

git commit

You can define a Git template by following these instructions.

Conclusion

To define a Git, commit message template, create the “.gitmessage” file inside your main project directory and define the template inside it. After that, execute the “git config –local commit.template .gitmessage” command to set the “.gitmessage” to default. Verify the results by committing a message in the directory. This write-up has demonstrated the steps to define a Git commit message template.

About the author

Abdul Mateen

I hold a bachelor's degree in Computer Science and have a good technical background. As an author, I break down technical concepts in a clear and concise manner. Learning new skills and exploring new technologies is my passion.