mefody.dev

Git Hooks: share with your team

In my previous chunk I showed the way to make development in your repo more productive. But there was a good question unsolved: “How to share git hooks with your team?” By default, all your .git files are local — they are not for being pushed. So how can I make my automation from .git/hooks public?

README.md

You can add a chapter to the README.md file with instructions on setting up git hooks locally. With links to gists or something like that. But this way is not the true way.

npm prepare

There is a way to add some automation to your package after npm install run. Npm has useful lifecycle scripts that can help you. The prepare script looks really suitable for our task to install git hooks on a local machine.

{
    "scripts": {
        "prepare": "bash ./copy-hooks.sh"
    },
}

But you still need to write your copy-hooks.sh script. Not a good way for frontend beginners.

Separate folder

To make git hooks a part of a repository, you can just move them to a separate folder. For example, to .githooks or .config/githooks. But you still need to have some steps that will make these hooks executable.

Git can help you with it. Command git config core.hooksPath .config/githooks tells your local git to use .config/githooks as a folder with hooks. Pretty simple.

{
    "scripts": {
        "prepare": "git config core.hooksPath .config/githooks"
    },
}

Konstantinos Leimonis proposes another trick with symlinks.

find .git/hooks -type l -exec rm {} \\;
find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \\;

This code links scripts from .githooks folder to .git/hooks/. So when git tries to call some hook from its default directory it will execute the hook from your separate directory.

Git hooks managers

There is a lot of projects that can help you to avoid bash learning if you just want to use web technologies for development. My favorite tools are:

With these managers, you don’t need to write any bash scripts, but you can. And their auto-installation is still simple. My package.json looks like this:

{
    "scripts": {
        "prepare": "npx simple-git-hooks"
    },
    "simple-git-hooks": {
        "pre-push": "npm run lint"
    }
}

Honestly, you don’t need all this prepare stuff for simple-git-hooks because it uses its own postinstall script and makes all the automation by itself. Husky had such automation before, but they decided to change the behavior.

Resources

Webmentions [?]

  1. В 293 выпуске подкаста «Веб-стандарты» @pepelsbey и @amel_true доказали мне, что я неправильно понимал, как работают git-хуки. Пришлось разобраться, как ими правильно делиться с командой. Получилась заметка в блог. А вы у себя хуки используете? Если да, то зачем?