Native Git Hooks: Bye Bye, Husky!

Rufat Khaslarov
JavaScript in Plain English
3 min readMay 8, 2024

--

Husky, a widely-used NPM package, has become the go-to choice for developers looking to integrate Git hooks into their projects. Boasting an impressive 10 million weekly downloads, showing widespread adoption in the developer community. Husky makes it easy to manage Git hooks in projects, making setup and maintenance effortless. But what if I told you there’s a simpler, more native alternative?

Let’s explore Git’s hooks and why they might just be the solution you never knew you needed.

Diving into the code.

When diving into the husky's code, we'll encounter fascinating lines that are worth exploring.

Especially, this one:

let { status: s, stderr: e } = c.spawnSync('git', ['config', 'core.hooksPath', `${d}/_`])

You may have noticed that it performs a git command to configure and update "core.hooksPath". It's quite unusual, isn't it?

By googling it, you will encounter the link to official git documentation https://git-scm.com/docs/githooks. Here you will find the following information:

Hooks are programs you can place in a hooks directory to trigger actions at certain points in git’s execution. Hooks that don’t have the executable bit set are ignored.

By default the hooks directory is $GIT_DIR/hooks, but that can be changed via the core.hooksPath configuration variable.

Guess what?

It seems that Husky is using git native hooks internally. Why can’t we do that on our own and remove it completely?

Let’s first create a folder for git hooks, let’s imagine .hooks in our repository. Please add the pre-commit file and grant it execution permission with chmod +x.

#!/bin/sh
echo "pre-commit hook run"

Now, let's open the package.json file and add the following NPM script:

...
"start": "node index.js",
"prepare": "git config core.hooksPath .hooks",
...

You may need to run npm install to run “prepare” script.

And that’s it.

Using native Git hooks in your repository ensures a consistent and easily accessible setup for all contributors, without the need for extra packages.

Instead of using Husky, consider using Git's native hooks now ♥️

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--