更改默认的 git hooks
不确定这在 git 中是否可行(我还没有找到它,但我可能在搜索中使用了错误的词汇),但是能够修改和启用钩子作为所有新存储库的默认值会很有用(在我的意思是创建时间),因此不必在每次创建新存储库时都进行自定义。似乎最简单的方法是编写一个包装器,在创建新存储库时设置我的钩子并对其进行 chmod,但如果 git 中内置了一种方法,我宁愿使用它,而不是使用不必要的包装器脚本,尽管周围很少。
从评论复制到现已删除的答案的澄清:
我的问题是是否可以更改所有新存储库的默认行为,因此不需要每次都以相同的方式自定义它们对于每个新存储库。简单的答案是编写一个包装器来创建和自定义存储库(它生成挂钩脚本并对其进行 chmod),但似乎这种默认行为也应该是可自定义的,而不必这样做。
Not sure if this is possible in git (I haven't found it but I may be using the wrong vocabulary in my searches), but it be would useful to be able to modify and enable hooks as the defaults for all new repositories (at the time of creation I mean) so these don't have to be customized each time a new repository is created. It seems the easy way to do this is write a wrapper that sets my hooks and chmods them when I create a new repository, but if there's a way built into git I would rather use that instead of having unnecessary wrapper scripts however little lying around.
Clarification copied up from a comment to a now-deleted answer:
My question is whether the default behavior for ALL new repositories can be changed, so they don't need to be customized in the same way each time for each new repository. The easy answer is write a wrapper for creating and customizing the repos (it generates the hook scripts and chmods them), but it seems like this default behavior should also be customizable without having to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 git 2.9 中,您有一个用于集中挂钩的新选项:
core.hooksPath
请参阅提交 867ad08, 提交de0824e, 提交 bf7d977,提交 49fa52f(2016 年 5 月 4 日),作者:Ævar Arnfjörð Bjarmason (
avar
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 6675f50,2016 年 5 月 17 日)git config
文档 现在包括:该路径的作者 (Ævar Arnfjörð Bjarmason
avar
)添加他的提交:Git 2.10 使用
git rev-parse --git-path hooks/
中的新设置请参阅 提交 9445b49(2016 年 8 月 16 日),作者:Johannes Schindelin (
dscho)
。
(由 Junio C Hamano --
gitster
-- 合并于 提交 d05d0e9,2016 年 8 月 19 日)--git-path的思想> 选项不仅是为了避免
始终使用
--git-dir
的输出为路径添加前缀,同时也尊重.git
目录内特定公共路径的覆盖(例如
git rev-parse --git-path objects
将报告环境变量
GIT_OBJECT_DIRECTORY
(如果设置)。With git 2.9, you have a new option made for centralizing hooks:
core.hooksPath
See commit 867ad08, commit de0824e, commit bf7d977, commit 49fa52f (04 May 2016) by Ævar Arnfjörð Bjarmason (
avar
).(Merged by Junio C Hamano --
gitster
-- in commit 6675f50, 17 May 2016)The
git config
documentation now includes:The author of this pathc (Ævar Arnfjörð Bjarmason
avar
) adds in his commit:Git 2.10 uses that new setting in
git rev-parse --git-path hooks/<hook>
See commit 9445b49 (16 Aug 2016) by Johannes Schindelin (
dscho
).(Merged by Junio C Hamano --
gitster
-- in commit d05d0e9, 19 Aug 2016)The idea of the
--git-path
option is not only to avoid having toprefix paths with the output of
--git-dir
all the time, but also to respect overrides for specific common paths inside the.git
directory(e.g.
git rev-parse --git-path objects
will report the value of theenvironment variable
GIT_OBJECT_DIRECTORY
, if set).来自
git- init
手册页(也适用于git-clone
(如果您要克隆现有存储库而不是从头开始创建新存储库):
您可以修改系统范围的模板目录(默认为
/usr/share/git-core/templates< /code>,但可能位于计算机上的不同位置),您可以在创建或克隆存储库时在命令行上提供
--template=
,或者您可以配置配置文件中的默认模板目录:From the
git-init
man page (also works withgit-clone
if you are cloning an existing repo instead of creating a new one from scratch):You can modify the system-wide template directory (which defaults to
/usr/share/git-core/templates
, but may be in a different location on your machine), you can supply--template=<template_directory>
on the command line when you create or clone the repo, or you can configure the default template directory in your config file:使用 git 1.6.5.3(和一些早期版本),您可以在
.git/hooks
目录中获得示例挂钩:它们在我的系统上都是可执行的。
要使用其中一个挂钩,请复制或重命名该文件并删除“
.sample
”后缀。根据您的要求进行适当编辑。解决评论中的问题 - 要更改安装的默认示例挂钩,您需要找到安装 git 的目录。在我的机器上,这是
$HOME/git
- 所以二进制文件位于$HOME/git/bin/git
中。那么包含示例钩子的目录是:如果您编辑这些模板(小心),那么这就是将复制到新的 git 存储库的内容。它们仍然是样品,但它们将是您的样品。
我没有尝试过在目录中创建非示例文件;它可能会也可能不会被复制。不过,要小心更改默认设置 - 当您下次升级时,您将必须重做更改。
With git 1.6.5.3 (and some earlier versions), you get sample hooks delivered in your
.git/hooks
directory:They are all executable on my system.
To make use of one of the hooks, either copy or rename the file removing the '
.sample
' suffix. Edit it as appropriate to suit your requirements.Addressing the question in a comment - to change the default sample hooks installed, you need to find the directory where
git
is installed. On my machine, that is$HOME/git
- so the binary is in$HOME/git/bin/git
. Then the directory containing the sample hooks is:If you edit those templates (be careful), then that is what will be copied to new git repositories. They'll still be samples, but they'll be your samples.
I've not experimented with creating a non-sample file in the directory; it might or might not be copied. Be wary of changing the defaults, though - when you next upgrade, you'll have to redo the changes.
对于 MacPorts doreory 将是
/opt/local/share/git-core/templates/
为了与团队成员共享挂钩或模板,我通常在其中创建子目录
类似 $PROJECT_DIR/lib/git/hooks 的项目
然后创建 Rake 任务或 shell 脚本
复制 cp $PROJECT_DIR/git/hooks/* $PROJECT_DIR/.git/hooks。
符号链接不起作用。
For MacPorts dorectory will be
/opt/local/share/git-core/templates/
To share hooks or templates with team members I usually create subdirectory in
project like
$PROJECT_DIR/lib/git/hooks
then create Rake task or shell script
to copy
cp $PROJECT_DIR/git/hooks/* $PROJECT_DIR/.git/hooks
.Symbolic links doesn't work.