DEV Community

Cover image for How to check commit message and branch name with git hooks without any new installation

Posted on • Updated on

How to check commit message and branch name with git hooks without any new installation


Hi All !
I'm a tech lead, and I'm on charge to check Pull Request/Merge Request on my team. And also to create release notes ( on each release.
So, my first problem was to resolve the commits of the developers, that they always have some mistake, or have errors into the commit message (without correct format), or errors in the branch name.
I searched and I found different solutions. A lot of them need to use an external software, like node (npm library), or php composer library, etc. And the projects are in different technologies, like Android, PHP, .NET, etc.

After checking all that I found, I created a solution that works in all environments without external dependencies.

The solution is really easy.
You need to follow these easy steps


  1. create .git-hooks folder into your project root directory, at the same level you already have .git folder
  2. create 2 files into this folder: pre-commit and prepare-commit-msg (these two files don't have an extension)
  3. put the correct code into each file (I will add them below these steps)
  4. run this command in your command line, into your main folder of your project (one level up from .git-hooks): git config core.hooksPath .git-hooks
  5. READY !

The Code

pre-commit file code:


BRANCH=$(git rev-parse --abbrev-ref HEAD)

if ! [[ $BRANCH =~ $REGEX ]]; then
  echo "Your commit was rejected due to branching name"
  echo "Please rename your branch with '(dev|release)-YYYY-qX.X.X' syntax"
  exit 1
Enter fullscreen mode Exit fullscreen mode

prepare-commit-msg file code:


MESSAGE=$(cat $1) 
COMMITFORMAT="^(feat|fix|docs|style|refactor|test|chore|perf|other)(\((.*)\))?: #([0-9]+) (.*)$"

if ! [[ "$MESSAGE" =~ $COMMITFORMAT ]]; then
  echo "Your commit was rejected due to the commit message. Skipping..." 
  echo ""
  echo "Please use the following format:"
  echo "feat: #1234 feature example comment"
  echo "fix(ui): #4321 bugfix example comment"
  echo ""
  echo "More details on"
  exit 1
Enter fullscreen mode Exit fullscreen mode

You can edit it according to your needs.


File pre-commit: check branch names.
In my case I filter to use only format like that:
Where YYYY is the year, and X.X.X is the version, in our case we use the Quarter number.
You could change that using regex and put what you want ;)

File prepare-commit-msg: check commit message.
In our case, we use the following format:

Off course, you could change it as your needs.

And finally, the command git config core.hooksPath .git-hooks change your local git hooks configuration to use the new path .

Top comments (8)

remcoboerma profile image
Remco Boerma

Thanks for sharing. Really useful example to demonstrate git commit hooks.

Since this solution requires bash, I assume python is available on every platform your team develops on. Have you seen python-semantic-release.readthedoc...?
Probably it's best suited for Python projects, but it does a lot of mundane work like keeping up the changelog.
Also it requires some installation of the command, which is of course not what you were looking for, but still I'm eager to know what you think of it.

anibalardid profile image

thanks for your comment !

Python is not available as "stock" in all environments. But also is a good alternative ;)

Besides it could be done with php or with npm, but how I said, we use php from a docker and commits are done outsite docker.

palinejs profile image

Thanks for share! I love it!

anibalardid profile image


olancheros profile image
Oscar Lancheros

This is a good article, thanks for sharing it!

anibalardid profile image

Your welcome Oscar !

eserkin profile image
Emiliano Serkin

Buenísimo el artículo . Me sirvió un montón

anibalardid profile image

gracias emi :)