您可以通过在 git-template-directory 中创建包含注释的默认-pre-push-脚本来实现此目的。
其中,模板目录包含挂钩模板,每次创建 $GIT_DIR 时都会将其复制到新存储库。每次本地 git-repository 由 git init 或 git clone 初始化时都会执行此操作。
示例
虽然git-init 的文档提供了不同的选项来定义一个 git-template,我将在这个例子中创建一个自定义的模板目录。
1。创建系统范围模板目录的副本
首先我们递归地复制一个默认模板目录到~/.git-templates/(你可以随意命名目录):
cp -r /Library/Developer/CommandLineTools/usr/share/git-core/templates ~/.git-templates
我正在使用 Mac - 所以我在 /Library/Developer/CommandLineTools/usr/share/git-core/templates 找到了一个(有多个目录)origin-template-directory,Linux 用户应该能够在 @ 找到系统范围的模板987654329@.
2。创建一个默认预推送钩子脚本
在~/.git-templates/hooks 中放入如下脚本,命名为pre-push 并使其可执行:
#!/bin/sh
printf "\033[0;31m Note to myself:\033[0m don't forget to replace the pre-push-hook. :)\n"
exit 1 # this hook should always fail
3。请参阅~/.gitconfig 中的新模板目录
替换或添加init.templatedir-选项,使其引用到您的新模板目录:
[init]
templatedir = ~/.git-templates/
4。测试你的新默认预推钩
现在每次初始化或克隆 git-repository 时,都应该得到定义的注释。
请注意,由于 Git 的新版本,源模板目录的内容将来可能会发生变化。在这种情况下,您可能需要更新模板目录的内容以消除讨厌的错误消息。