6

I recently updated my pre_commit.sample file to run rubocop upon a commit (renamed to pre_commit).

#!/bin/sh
#
# Check for ruby style errors
rake run:rubocop

Mistakingly I thought this would update on other developers machines when pulling the changes. How can i ensure that anyone who pulls the change has their pre_commit file updated

Thanks

Richlewis
  • 15,070
  • 37
  • 122
  • 283

2 Answers2

9

Up until git v2.9 there was no way to update hooks on client side.

Git v2.9 exposed new configuration to allow this with this:

git config [--local] core.hooksPath ...

core.hooksPath By default Git will look for your hooks in the $GIT_DIR/hooks directory. Set this to different path, e.g. /etc/git/hooks, and Git will try to find your hooks in that directory, e.g. /etc/git/hooks/pre-receive instead of in $GIT_DIR/hooks/pre-receive.

The path can be either absolute or relative.
A relative path is taken as relative to the directory where the hooks are run.

This configuration variable is useful in cases where you’d like to centrally configure your Git hooks instead of configuring them on a per-repository basis, or as a more flexible and centralized alternative to having an init.templateDir where you’ve changed default hooks.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • thanks for your help, so to clarify i would run this locally and commit ? apologies not quite understanding 100% – Richlewis Nov 11 '16 at 15:08
  • You have to set it locally on the machine and specify a path to the hooks. since the path can be outside your working directory you can update it and the "clients" will get your updated. Without this configuration it cannot be done unless using 3rd party script – CodeWizard Nov 11 '16 at 15:19
  • 1
    As an idea: you can set this config to a local non-`.git` folder, a shared network folder, or a folder synced by OneDrive/Dropbox, whatever you like! – MS Berends Aug 17 '22 at 08:13
2

As I mentioned in "Git commit hooks - global settings" and "change default git hooks" last May 2016, Git 2.9 and 2.10 introduced git config core.hooksPath.

That seems to confuse the OP:

If I have an existing repo and want all other dev's who pull changes to have an updated pre-commit hook for example how would I do this ?
Within the repo there is /.git/hooks/pre_commit, can I point it to that

More precisely, within a git repo, there is a /.git/hooks/pre-commit.sample and, considering you want a common pre-commit hook for all developers, you should not make and then point to a /.git/hooks/pre_commit script within your local repo.

All developers have to reference the same gobal shared network-accessible path used with git config core.hooksPath \\a\common\shared\path: that setting needs to be activated on each developer workstation, either within their repo (local setting) or for all their repo (global setting: git config --global core.hooksPath \\a\common\shared\path.

(I use here a Windows UNC syntax, use the one suited for your OS)

Once they all reference the same path, you can set up your pre-commit hook there:

 \\a\common\shared\path\pre-commit

Then you can update that script (the one accessed by everyone), allowing all developers to benefit instantly of the updates.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250