swanson
swanson

Reputation: 7482

Applying a git post-commit hook to all current and future repositories

I've written a Git post-commit hook and it works correctly. However, I want to add this hook to apply to all current (and future) Git repositories I am working on. I tried adding the hook to my ~/.git/hooks/ directory instead of in the hooks directory in the project directory, however, this did not seem to work.

Is there a way to create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)? If not, what would be the best solution going forward -- perhaps a git-init template?

Upvotes: 344

Views: 111440

Answers (5)

yegor256
yegor256

Reputation: 105193

This is what I did on my laptop:

  1. Instruct Git to run pre-commit hook from ~/.githooks, as suggested earlier:
git config --global core.hooksPath ~/.githooks
  1. Create an executable pre-commit file over there (need to have pre-commit installed):
#!/bin/bash
set -ex
pre-commit run --config ~/.githooks/config.yaml
  1. Create ~/.githooks/config.yaml for pre-commit (just an example, my actual config file is larger than this one):
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
    -   id: check-added-large-files
    -   id: check-toml
    -   id: check-xml
    -   id: check-yaml
    -   id: trailing-whitespace

Now, in all my Git repositories the hook works (no need to configure each and every repository).

Upvotes: 6

sklnd
sklnd

Reputation: 4723

As of Git 1.7.1, you can set init.templatedir in your gitconfig to tell Git where to look for templates.

Set it like this:

mkdir ~/.git_template && \
git config --global init.templatedir '~/.git_template'

Afterward, new repositories you create or clone will use this directory for templates. Place the hooks you want in ~/.git_template/hooks. Existing repositories can be reinitialized with the proper templates by running git init in the same directory .git is in.

For Git versions older than 1.7.1, running git init --template ~/.git_template will work if you're like me and still want to manage your .git_template directory along with the rest of your dot files. You can also use the $GIT_TEMPLATE_DIR environment to tell git init where your template directory is.

Upvotes: 329

VonC
VonC

Reputation: 1328712

I want to add this hook to apply to all current (and future) git repositories I am working on

With git 2.9+ (June 2016), all you would do is:

git config --global core.hooksPath /path/to/my/centralized/hooks

See "change default git hooks": this has been done to manage centralized hooks.


But, as noted by Adam Lindberg in the comments:

setting a global hooks path disables all local hooks in you repos!

Depending on your use case, that can be the goal, if you want to, as the OP puts it, "create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)".

But if you are unaware of that side effect, and still want to retain some local hooks... those would be ignored when core.hooksPath is set.

Upvotes: 313

Neil Best
Neil Best

Reputation: 823

A minimalist approach is to create a git_hooks/ directory in your repository to track the hooks that you write for that project, bring it to the attention of future users by mentioning it in a README, and rely on them to do the right thing after they have cloned. I have cogitated on this for a while and chose an incremental approach. Down the road I might consider using a tool like git-hooks.

Upvotes: 4

Cascabel
Cascabel

Reputation: 497552

If you want them everywhere on your system (including users besides you), you can modify the contents of the installed template directory - those are in $PREFIX/share/git-core/templates/hooks, where $PREFIX is probably /usr/local or /usr.

If you want this to just be for you, then yes, the simplest thing would be the --template option of git-init. You could easily keep a personal template directory which has symlinks back to the installed version of defaults you want to keep (individual hooks, the info directory...) and then your own content in hooks/post-commit and anything else you want to customize.

Upvotes: 74

Related Questions