Skip to main content

Git hooks

Note

Please note that supporting githooks using husky comes by default with our template.

Git hooks are scripts executed at specific times during the Git lifecycle. These occur at various commit stages, such as the pre-commit and post-commit phases (post-commit).

These are beneficial because they enable programmers to execute custom tasks or even enforce standards by automating the execution of other scripts.

A great library provides an easy configuration called Husky to execute these scripts.

Please read the following paragraph to know how to add your scripts to be used by git-hooks.

Add scripts to run with git hooks:

In this example, we will run commit lint for the commit message and run test and lint commands in the pre-commit phase.

You just need to create the following files that will help you execute these commands:

.husky/commit-msg
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit ""
.husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn lint
yarn test

If you want to run more commands, you just need to add them to the files above or create a new file for running a script in a different phase of the git cycle.

Sources:

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#_client_side_hooks

https://github.com/typicode/husky

https://github.com/conventional-changelog/commitlint