在版本控制上更新 pre_commit 钩子文件

Update pre_commit hook file on version control

我最近在提交时将我的 pre_commit.sample 文件更新为 运行 rubocop(重命名为 pre_commit)。

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

我错误地认为这会在拉取更改时在其他开发人员机器上更新。我如何确保拉动更改的任何人都更新了他们的 pre_commit 文件

谢谢

在 git v2.9 之前,无法在客户端更新挂钩。

Git v2.9 公开了新配置以允许此操作:

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.

正如我在 2016 年 5 月的“Git commit hooks - global settings" and "change default git hooks”中提到的,Git 2.9 和 2.10 引入了 git config core.hooksPath

好像是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

更准确地说,在 git 存储库中,有一个 /.git/hooks/pre-commit.sample 并且,考虑到您想要 all 的通用 pre-commit 挂钩开发人员,您应该 而不是 创建然后指向本地存储库中的 /.git/hooks/pre_commit 脚本。

所有开发人员都必须引用与 git config core.hooksPath \a\common\shared\path 一起使用的相同的全球共享网络可访问路径:需要在每个开发人员工作站上激活该设置,无论是在他们的回购(本地设置)还是他们所有的回购(全局设置:git config --global core.hooksPath \a\common\shared\path.

(我这里用的是Windows UNC syntax,用适合你的OS)

一旦它们都引用相同的路径,您就可以设置预提交挂钩 那里:

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

然后您可以更新该脚本(每个人都可以访问的脚本),让所有开发人员立即受益于更新。