DEV Community

Hector de Isidro
Hector de Isidro

Posted on • Updated on

How good are Git Hooks?

I’ve tried them and the answer will shock you

As developers we use Git (or at least we should) an uncountable number of times
a day¹ so, because not all of us are machines yet, we can sometimes make a mistake as the result of the rush (i.e pushing a commit to master when it should go to dev). To try to cut down these kinds of slips we have Git Hooks’ help.

What are Git Hooks?

Git Hooks are extensionless executable scripts² you can place in your repository’s hooks folder (by default .git/hooks)³ to trigger actions at certain points in Git’s execution. In other words: we can automate what we always want to happen before/after using a git command.

There is a huge list of hook types⁴ you can attach⁵ scripts to but here we will see just a few.

Tip: they can always be bypassed adding the--no-verify param

I hope the code snippets attached below are self-explanatory.

Pre-commit

Commit-msg

Pre-push

As a good practice we could run our tests at this point as well 😉

Sharing hooks with our lovely team

As the default hooks folder belongs to .git folder which isn’t versioned we can use one of these strategies depending on our Git version⁶ to share hooks among our team members:

  • version 2.9 or greater:

Tip: you can apply this configuration to all current (and future) repositories adding the --global param

  • earlier version than 2.9:

Said that, you can use this script I’ve created for lazy people like me:

Another additional benefit of using a different folder than the default one is that we can keep them updated

Introducing Git Hooks⁷ repository

In order to be able to run multiple hooks within each type avoiding the creation of a huge and cumbersome single script loaded of comments, I have created a public repository which offers a new structure to store our hooks: each type of hook (i.e. pre-commit) launches the scripts we have stored in its corresponding subfolder (i.e /pre-commit-hooks).

To disable or enable a hook you can use the git-hooks-state.sh script (which basically is a chmod alias that changes their executable permission depending on the new state indicated)

I wish I didn’t have to say it but PRs are very welcome.

PS: you will have a lot more spare time to configure your hooks if you start using LolaMarket to shop for groceries 😄

This article was originally published on Medium


[1] Remember, commit early and often.
[2] Use chmod +x script.sh from terminal to make a script executable
[3] Every Git repository has a hooks folder which contains some disabled sample hook files but IMHO they are pretty useless 🔥. You can checkout the latest version here
[4] They are usually categorized into two main types: client side and server side
[5] It supports any scripting language that your system knows how to execute
[6] Use git --version to find out your current version of Git
[7] Great naming, isn’t it? 😞


External links 👀

Top comments (0)