Can Git hook scripts be managed along with the repository?

In Git 2.9, the configuration option core.hooksPath specifies a custom hooks directory.

Move your hooks to a hooks tracked directory in your repository. Then, configure each instance of the repository to use the tracked hooks instead of $GIT_DIR/hooks:

git config core.hooksPath hooks

In general, the path may be absolute, or relative to the directory where the hooks are run (usually the working tree root; see DESCRIPTION section of man githooks).


Theoretically, you could create a hooks directory (or whatever name you prefer) in your project directory with all the scripts, and then symlink them in .git/hooks. Of course, each person who cloned the repo would have to set up these symlinks (although you could get really fancy and have a deploy script that the cloner could run to set them up semi-automatically).

To do the symlink on *nix, all you need to do is:

root="$(pwd)"
ln -s "$root/hooks" "$root/.git/hooks"

use ln -sf if you're ready to overwrite what's in .git/hooks


For Node.js users a simple solution is to update package.json with

{
  "name": "name",
  "version": "0.0.1",
  ......
  "scripts": {
    "preinstall": "git config core.hooksPath hooks",

The preinstall will run before

npm install

and redirects Git to look for hooks inside the .\hooks (or whatever name you choose) directory. This directory should mimic .\.git\hooks in terms of file name (minus the .sample) and structure.

Imagine Maven and other build tools will have an equivalent to preinstall.

It should also work across all platforms.

If you need any more information, see Two ways to share Git hooks with your team.


If your project is a JavaScript project and you use npm as the package manager, you can use shared-git-hooks to enforce Git hooks on npm install.

Full disclosure: I wrote this package


Most of the modern programming languages, or rather their build tools, support plugins to manage Git hooks. That means all you need to do is configure your package.json, pom.xml, etc. files, and anyone in your team will have no option but to comply unless they change the build file.

The plugin will add content to .git directory for you.

Examples:

Git Build Hook Maven Plugin

githook-maven-plugin

git-hooks-js