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"
git commit -m "fix"
git commit -m "it'll work this time!"
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
Then update your Git configuration. You can do this one of 2 ways:
git config --global commit.template ~/.config/git/commit.txt
Or you can update your configuration file:
[commit]
template=~/.config/git/commit.txt
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)]
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
Commit message with !
to draw attention to breaking change
refactor!: drop support for Node 6
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.
Commit message with no body
docs: correct spelling of CHANGELOG
Commit message with scope
feat(lang): add polish language
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
Top comments (1)
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.