Enforce a Git Commit Message Format


Enforce a git commit message format for your project.

Updated: 28 Aug 2017 to use commitmsg hook. Thanks to marionebl.

Power of a Convention

You could choose any format. One git commit message format that I like is conventional commits. With a convention, you can generate a changelog or use tools like lerna to automatically determine what package versions to publish.

Commitlint

In order to get the automation benefits, the commits have to be consistent and predictable. You need to ensure they follow the convention. There are a few tools that do this. One nice one is Commitlint.

Using this tool you can set one of about 2 dozen rules about your commit message. These coincide nicely with conventional commit message segments.

You can run commitlint via cli:

npm install @commitlint/cli
echo "Some commit message" | commitlint

Checking Message on Commit

However, you're not usually going to want to run the cli on an echoed string. We want to write a commit message in our regular git workflow, it would fail the formatting check, and you'd be denied the privelege to commit until you'd committed with a well-formatted message.

To do this you'll want a package that helps you handle commit hooks:

npm install husky --save-dev

And add a script to package.json:

"scripts": {
  "commitmsg": "commitlint -e $GIT_PARAMS",
}

Using the -e flag, commitlint will automatically go to the COMMIT_EDITMSG, your latest commit message, to do its linting.

In the case of bad commit, you'll get the errors telling you what formatting rules you need to fix to let your commit through.

commitlint output

How do you ensure a conventional commit message format on your projects?