change default git hooks

Viewed 32760

Not sure if this is possible in git (I haven't found it but I may be using the wrong vocabulary in my searches), but it be would useful to be able to modify and enable hooks as the defaults for all new repositories (at the time of creation I mean) so these don't have to be customized each time a new repository is created. It seems the easy way to do this is write a wrapper that sets my hooks and chmods them when I create a new repository, but if there's a way built into git I would rather use that instead of having unnecessary wrapper scripts however little lying around.


Clarification copied up from a comment to a now-deleted answer:

My question is whether the default behavior for ALL new repositories can be changed, so they don't need to be customized in the same way each time for each new repository. The easy answer is write a wrapper for creating and customizing the repos (it generates the hook scripts and chmods them), but it seems like this default behavior should also be customizable without having to do that.

4 Answers

From the git-init man page (also works with git-clone if you are cloning an existing repo instead of creating a new one from scratch):

       --template=<template_directory>
           Provide the directory from which templates will be used. The
           default template directory is /usr/share/git-core/templates.

           When specified, <template_directory> is used as the source of the
           template files rather than the default. The template files include
           some directory structure, some suggested "exclude patterns", and
           copies of non-executing "hook" files. The suggested patterns and
           hook files are all modifiable and extensible.

You can modify the system-wide template directory (which defaults to /usr/share/git-core/templates, but may be in a different location on your machine), you can supply --template=<template_directory> on the command line when you create or clone the repo, or you can configure the default template directory in your config file:

[init]
     templatedir = /path/to/templates

With git 1.6.5.3 (and some earlier versions), you get sample hooks delivered in your .git/hooks directory:

$ ls .git/hooks
applypatch-msg.sample     post-update.sample        prepare-commit-msg.sample
commit-msg.sample         pre-applypatch.sample     update.sample
post-commit.sample        pre-commit.sample
post-receive.sample       pre-rebase.sample
$ 

They are all executable on my system. To make use of one of the hooks, either copy or rename the file removing the '.sample' suffix. Edit it as appropriate to suit your requirements.


Addressing the question in a comment - to change the default sample hooks installed, you need to find the directory where git is installed. On my machine, that is $HOME/git - so the binary is in $HOME/git/bin/git. Then the directory containing the sample hooks is:

$HOME/git/share/git-core/templates/hooks

If you edit those templates (be careful), then that is what will be copied to new git repositories. They'll still be samples, but they'll be your samples.

I've not experimented with creating a non-sample file in the directory; it might or might not be copied. Be wary of changing the defaults, though - when you next upgrade, you'll have to redo the changes.

Related