DEV Community

Mohamed Saleh
Mohamed Saleh

Posted on • Originally published at blog.salehm.dev

Consistent Git Commit Messages Using a Template

One way to improve the consistency of your commits is to use a git template.

Any time you start a new commit, this template will be pre-loaded.

That means you'll be able to use git commit comments with some guidelines and reminders!

This can be useful because it can help remind you of things you typically would include in a git commit message; it could even act as a line length guide!

Note: You don't need to comment out all the contents of your git commit template.

This is simply a common usage but not required... but why would you want all of your commits messages to have the same message? 🙂

Usage

First off, in your home directory, create a file with a commonly used name: .gitmessage. <- This is where you'll store the contents of this template.

Here's an example template that I personally use:

# Commit title - 50 characters ------------------|


# Commit body - Line Width 72 characters ------------------------------|


# References, issue #, ticket #, etc:


# Any co-authors:
# Co-authored-by: LastName, FirstName <email@email.com>

Enter fullscreen mode Exit fullscreen mode

Note: the line length guides only work for monospaced fonts, i.e., each character has the same width.

I prefer it short and to the point -- but you can include whatever you like in here.

Now, the last step to make it automatically pre-populate your git commit messages is to set it in your git config.

To do it through the command line, run:

git config --global commit.template ~/.gitmessage
Enter fullscreen mode Exit fullscreen mode

OR

You can manually set the commit template through your global .gitconfig file.

e.g.: image.png

Wrapping Up

You should be all set!

Now every time you git commit in your project repo, you'll get a pre-loaded template that you can edit for your commit message.

Hope this helps you write better git commits!

References

Learn more about git best practices:

A more in-depth guide about git commit templates:

Top comments (0)