Git hooks management

Viewed 7838

We use custom-written Git hooks in our project.

Hooks are stored in a project's repository, and, when they do change, to get a new version each user must copy them manually in his .git/hooks directory. This is rather inconvenient.

One way to improve this is to make .git/hooks a symlink into worktree. But this would imply that each branch (even user's local feature branches under development) should have the most current version of hooks. This is not convenient as well.

How would you solve the problem?

7 Answers

Maintain a separate repository of your hooks and symlink into that.

I agree, it'd be nice if Git had a built-in mechanism for propagating hooks scripts but it doesn't.

pre-commit has a bunch of features that look promising

  • Works for many languages, and has a plugin architecture to add support for other languages
  • Capable of bootstrapping the hook dependencies (language dependent)
  • Applies hooks incrementally (only run hooks on changes that are part of the commit)
  • Leverages the init.templateDir so newly checked out repos automatically install the correct hooks
  • Temporarily disable a specific hook using an environment variable - don't skip all the checks just because you can't run one of them

Source code here

We made .git/hooks a symlink into the working tree.

For those rare occasions when someone needs to commit files that the hooks will reject, we use git commit --no-verify

Related