Enforcing Git Commit Message Style

Austin Cunningham
2 min readFeb 12, 2019

I was working on a project that wanted commit messages in the following formats

feat(feature-name): message text here (AEROGEAR-Number)
fix(feature-name): etc...
docs(feature-name): etc...
breaking(feature-name): etc...

This was a pain to enforce and check manually so we decided to automate the check for this. Firstly I wrote a script to check the existing branch commits and see if they match the format

I copied the script to a script directory of the root of the project and was initially using it with Circle ci to check the commit during a build.

steps:      
- checkout
- run: ./scripts/commit-filter-check.sh

It was decided that a local check would more useful, we then decided to use githooks to run the script. There is a .git/hooks directory in every git project with sample git hooks.

Remove the .sample and the hook script becomes live in this case I used the commit-msg git hook and use it to run my script. The hook is triggered by a failure with an exit 1

Only issue was the .git directory never gets seen by git commit. So I needed a way to push my changes and allow others to use them. Moving commit-msg file to a .githook directory allows it to be committed. You can then add a line to the setup script of the project to create a sym link to the local .git/hooks directory

ln -sf $$PWD/.githooks/* $$PWD/.git/hooks/

Now every commit message is checked and will fail if it doesn’t match the format

My Blog

--

--