Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

githooks

hook

People also ask

What is post-commit hook in git?

The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.

Are git hooks pushed to repo?

No. Hooks are per-repository and are never pushed. Similarly, the repo config isn't pushed either, nor is anything in .


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:

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 dir 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.


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.


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.


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.