Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Héctor de Isidro
Héctor de Isidro

Posted on • Edited on

     

How good are Git Hooks?

I’ve tried them andthe answer will shock you

As developers we use Git (or at least we should) an uncountable number of times
a day¹ so, becausenot 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 haveGit Hooks’ help.

What are Git Hooks?

Git Hooks areextensionless 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 canautomate what we always want to happen before/after using agit command.

There is a hugelist 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 apublic 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 thegit-hooks-state.sh script (which basically is achmod 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 usingLolaMarket to shop for groceries 😄

This article was originally published onMedium


[1] Remember, commit early and often.
[2] Usechmod +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 versionhere
[4] They are usually categorized into two main types: client side and server side
[5] It supports any scripting language thatyour system knows how to execute
[6] Usegit --version to find out your current version of Git
[7] Great naming, isn’t it? 😞


External links 👀

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Spain
  • Joined

More fromHéctor de Isidro

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp