Using Git Hooks With Ghooks

Using git hooks with ghooks

Git hooks provides you the ability execute your custom scripts when a specific event has been triggered (like commit, push etc). You can utilize this feature to improve your development process in various ways. For example, you can run your test cases on pre-push hook and prohibit user from pushing the changes if any of the test cases fails.

But there is a tiny problem! All of your hooks are placed within your .git directory and the files inside this directory cannot be versioned i.e these files cannot be committed and pushed. So, you have to manually share these files with every member of your team. However, You can easily overcome this issue using a npm package called ghooks.

Ghooks:

To install ghooks use the following command:

npm i --save-dev ghooks

To associate a particular script with a git hook, you can use the config object within your package.json file. For example, following code will execute the test cases each time user tries to push his changes. And if any of the test cases fails, the git hook will abort the push process. This can be helpful to prevent users from pushing broken code to the git repository.

"config": {
  "ghooks": {
     "pre-push": "npm run specs"
   }
 }

And whenever someone clones your project and downloads all dependencies using npm install, ghooks will also be installed with other packages. Also, that person will have access to your config object via the package.json file.

You can also execute more than one script at the same time. For example:

"pre-push": "npm run specs && npm run eslint"

For more information: https://git-scm.com/docs/githooks

Leave a Reply

Your email address will not be published. Required fields are marked *