Global git hook

githook

Is it possible to hook all ways of creating git repo? So I can run script when repo is cloned, initialized… (are there any other ways, except moving/copying folder?).

My intention is to track all repos, so I don't need to go through all dirs to run some action (mainly git status as I sometimes forget to commit changes and git gc --auto).

Best Answer

Hooks are (currently) exclusively configured per-repository, so there can be no active hooks before a repository is created.

If you just want to initialize some repository specific settings identically for all new repositories you might be able to use the repository template mechanism.

By using your own template you can

  • install activated hooks (by providing a <template‑dir>/hooks/<hook‑name> file),
  • set configuration variables (by providing a <template‑dir>/config file), and
  • configure per-repository exclude rules or attributes (by providing a <template‑dir>/info/exclude or <template‑dir/info/attributes> file).

Any configuration file that lives in a .git directory can be placed in a template to serve as the initial copy for that file in new repositories initialized from the template. I suppose you could even provide initial objects and refs.

Once you have a template directory made up with your customizations you must tell git init and git clone where to find it. This can be done explicitly with the --template option (Git 0.99.4 or later), or implicitly with the GIT_TEMPLATE_DIR environment variable (Git 1.5.0 or later), or implicitly by setting the init.templatedir configuration variable in the “global” (per-user) or “system” Git configuration files (i.e. ~/.gitconfig or /etc/gitconfig (varies by installation); Git 1.7.1 or later).

Related Question