Version Control Git Hooks with npm Scripts

There are lots of great primers on Git hooks out there, so if you’re new to the concept, take a little time to get comfortable first; we’ll get straight to the point here.

The Problem

By default, Git stores client-side hooks in .git/hooks which is outside of version control. While packages like Husky are available to make creating and versioning hooks easy, I found the setup process for the newest version too close to a “roll your own” method to justify learning yet another CLI.

The Solution

Create a directory at the project root to contain the hooks. I prefer to keep mine at ./scripts/git/hooks, but you can choose whatever path best fits the project’s file structure.

Git provides a configurable setting called hooksPath in the core table of the .git/config file and commands to set and unset it. In package.json add:

{
  "scripts": {
    "hooks:install": "git config core.hooksPath ./scripts/git/hooks",
    "hooks:uninstall": "git config --unset"
  }
}

And that’s it! After running the hooks:install script, Git recognizes valid client-side hooks in ./scripts/git/hooks and more importantly, they are version controlled.

Optionally, you can include npm run hooks:install in a separate bootstrap script to combine and run any all of the processes needed to bootstrap a project.

There are certainly other methods for achieving this behavior, but this solution felt particularly elegant to me. Hope it’s useful and as always, feel free to point out any edge cases or your own preferred solution.