如何将我的预提交钩子添加到现有的git存储库中 [英] How can I add my pre-commit hook to an existing git repository

查看:75
本文介绍了如何将我的预提交钩子添加到现有的git存储库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预提交的挂钩,我想将其添加到存储库中,以便通过签出,我的同事可以立即将其安装到位.
但是,如果我尝试添加它(位于项目的根目录中),则会得到以下结果:

I have a pre-commit hook and I want to add it to the repository so that by checking it out my colleagues have it instantly in place.
However if I try to add it ( being in the root directory of my project) I get the following result:

$ git add  .git/hooks/pre-commit
error: Invalid path '.git/hooks/pre-commit'
error: unable to add .git/hooks/pre-commit to index

您知道这是否可行以及如何实现我的目标吗?

Any idea if this work and how to achieve my goal?

推荐答案

检查一下,我的同事可以立即将其安装到位

checking it out my colleagues have it instantly in place

9月. 2015 :这是不可能的:可以在源代码管理中放入一个钩子(您只需将脚本复制到git repo中),但是在克隆/签出时它不能自动就位"(活动):太危险了,具体取决于钩子实际在做什么.
另请参见" Git远程/共享的预提交钩子"

Sept. 2015: That is not possible: a hook can be put in source control (you simply copy the script in your git repo), but it cannot be "automatically in place" (active) on clone/checkout: that would be way too dangerous, depending on what the hook is actually doing.
See also "Git remote/shared pre-commit hook"

您仍然需要通过将符号链接(即使在Windows中)添加到git repo中存在的预提交钩子来激活它.

You would still need to activate it, by adding a symlink (even on Windows) to the pre-commit hook present in the git repo.

2016年12月更新: OP Calamity Jane 提到

我现在已经在symfony2项目中解决了它(并与其他项目一起使用),将其作为composer.json的一部分.
因此,如果同事正在执行composer installcomposer update,则会自动将其放置在正确的位置.

I solved it by now in symfony2 projects (and with others, it also should work) to have it as part of the composer.json.
So if a colleague is doing a composer install or composer update, it is automatically placed in the correct place.

"scripts": { "dev-install": [ "bash setup_phpcs.sh" ] }, 

因此在开发人员中,会自动调用setup_phpcs.sh并将钩子从存储库中的文件夹复制到正确的位置.
而且由于该挂钩是存储库的一部分,因此可以轻松地对其进行更新和分发.

So on dev, setup_phpcs.sh is automatically called and that copies the hook from a folder in the repository to the right place.
And since the hook is part of the repository it can be easily updated and distributed.

Hi-Angel 所述/32649628/如何添加我的预提交钩子以连接到现有的git存储库/32649801?noredirect = 1#comment98243078_32649801>评论:

As noted by Hi-Angel in the comments:

我发现:Emacs具有带有钩子的build-aux目录,并且在运行autogen.sh时,所有钩子都从那里复制.

I figured that out: Emacs has build-aux dir with hooks, and, upon running autogen.sh, all hooks are copied from there.

这篇关于如何将我的预提交钩子添加到现有的git存储库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆