DEV Community

Cover image for Tip 4: Git Commit Templates & Conventional Commits
David McKay
David McKay

Posted on

Tip 4: Git Commit Templates & Conventional Commits

Git commit messages are important. We all know that, yet ... in the heat of the moment, we're prone to this non-sense:

git commit -m "stuff"
Enter fullscreen mode Exit fullscreen mode
git commit -m "fix"
Enter fullscreen mode Exit fullscreen mode
git commit -m "it'll work this time!"
Enter fullscreen mode Exit fullscreen mode

Any of them look familiar? You bet your ass they do.

Adding a Git Commit Template

This is nice and easy. First, create a text file for your template inside your ~/.config/git directory (~/.git/ on macOS I think).

# ~/.config/git/commit.txt
My Commit Template
Enter fullscreen mode Exit fullscreen mode

Then update your Git configuration. You can do this one of 2 ways:

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

Or you can update your configuration file:

[commit]
template=~/.config/git/commit.txt
Enter fullscreen mode Exit fullscreen mode

Conventional Commits

Now that you've got a simple template. What should you put there?

Let me introduce you to Conventional Commits.

You can find my template here.

The following is the summary and some examples from the website linked above.


Conventional Commits

The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.

The commit message should be structured as follows:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]
Enter fullscreen mode Exit fullscreen mode

Examples

Commit message with description and breaking change footer
feat: allow provided config object to extend other configs

BREAKING CHANGE: `extends` key in config file is now used for extending other config files
Enter fullscreen mode Exit fullscreen mode
Commit message with ! to draw attention to breaking change
refactor!: drop support for Node 6
Enter fullscreen mode Exit fullscreen mode
Commit message with both ! and BREAKING CHANGE footer
refactor!: drop support for Node 6

BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.
Enter fullscreen mode Exit fullscreen mode
Commit message with no body
docs: correct spelling of CHANGELOG
Enter fullscreen mode Exit fullscreen mode
Commit message with scope
feat(lang): add polish language
Enter fullscreen mode Exit fullscreen mode
Commit message with multi-paragraph body and multiple footers
fix: correct minor typos in code

see the issue for details

on typos fixed.

Reviewed-by: Z
Refs #133
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
vhs profile image
vhs

I was unable to view your template without signing up to GitLab. Is the code open source anywhere else that doesn't require a login to access it?

Here's an alternative Conventional Commit template I found on GitHub: gist.github.com/RangHo/17fc8ea229f....

For those who don't want to use the commit template globally (that's rather opinionated) local to a given project as described here.