“Control your commits” © Git Hook

Alexey Alter-Pesotskiy
testableapple
Published in
2 min readJun 1, 2019

This Note originally published on my Personal Blog here. Read original note so that you won’t miss any content.

Git Hooks are really useful guys, that help us to control interactions with Git. I would like to show you the couple samples of their usage.

Precondition

First of all we should:

  • create .githooks folder near the .git folder
  • create Makefile in the project’s root, that will set githooks path and activate them
  • fill Makefile like in the sample below:

To force Makefile works, just enough to execute this command in the terminal:

$ make

Commit message

If we are working in a feature branch workflow it would be pretty nice to have an issue name in an every commit developer does. To do that we need:

  • create commit-msg file into the .githooks folder
  • add a regex into the commit-msg file, that will be verify the commits messages:

User email

Imagine that we would like each developer to use only corporation emails. So, to do that we need:

  • open still the same commit-msg file in the .githooks folder
  • add a regex into commit-msg file, that will be verify an user’s email:

Conclusion

Eventually I would like to highlight, that Git Hooks give us a soil (or better to say hooks?) for automation. Whether it be an issue names collection for release or a simple censure in the commit messages.

--

--