将 git post-commit 挂钩应用于所有当前和未来的存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2293498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applying a git post-commit hook to all current and future repos
提问by swanson
I've written a Git post-commit hook and it works correctly. However, I want to add this hook to apply to all current (and future) git repositories I am working on. I tried adding the hook to my ~/.git/hooks/
instead of in the hooks directory in the project directory, however, this did not seem to work.
我写了一个 Git 提交后钩子,它工作正常。但是,我想添加这个钩子以应用于我正在处理的所有当前(和未来)git 存储库。我尝试将钩子添加到我的~/.git/hooks/
而不是项目目录中的钩子目录中,但是,这似乎不起作用。
Is there any way to create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)? If not, what would be the best solution going forward -- perhaps a git-init template?
有什么方法可以创建适用于我系统上所有存储库的全局 Git 挂钩(无需将它们复制到每个项目目录中)?如果不是,那么最好的解决方案是什么——也许是 git-init 模板?
采纳答案by VonC
I want to add this hook to apply to all current (and future) git repositories I am working on
我想添加这个钩子以应用于我正在处理的所有当前(和未来)git 存储库
With git 2.9+ (June 2016), all you would do is:
使用 git 2.9+(2016 年 6 月),您要做的就是:
git config --global core.hooksPath /path/to/my/centralized/hooks
See "change default git hooks": this has been done to manage centralized hooks.
请参阅“更改默认的 git 钩子”:这是为了管理集中式钩子而完成的。
回答by sklnd
As of git 1.7.1, you can set init.templatedirin your gitconfig to tell git where to look for templates.
从 git 1.7.1 开始,您可以在gitconfig中设置init.templatedir 以告诉 git 在哪里查找模板。
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.
之后,您创建或克隆的新存储库将使用此目录作为模板。把你想要的钩子放在~/.git_template/hooks
. 通过git init
在同一目录中运行,可以使用适当的模板重新初始化现有存储库.git
。
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.
对于早于 1.7.1 的 git 版本,git init --template ~/.git_template
如果您像我一样并且仍然想管理您的.git_template
目录以及其余的点文件,运行将起作用。您还可以使用$GIT_TEMPLATE_DIR
环境来判断git init
您的模板目录在哪里。
回答by Cascabel
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
.
如果您希望它们在您的系统上的任何地方(包括除您之外的用户),您可以修改已安装模板目录的内容 - 那些在 中$PREFIX/share/git-core/templates/hooks
,其中$PREFIX
可能是/usr/local
或/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.
如果你想让它只适合你,那么是的,最简单的事情就是--template
git-init 选项。您可以轻松保留一个个人模板目录,该目录具有指向您要保留的默认设置的已安装版本的符号链接(个人挂钩、信息目录...),然后是您自己的内容hooks/post-commit
以及您想要自定义的任何其他内容。
回答by Neil Best
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.
一种极简的方法是git_hooks/
在您的存储库中创建一个目录来跟踪您为该项目编写的钩子,通过在 中提及它来引起未来用户的注意README
,并在他们克隆后依靠他们做正确的事情。我已经考虑了一段时间并选择了增量方法。以后我可能会考虑使用像git-hooks这样的工具。