简体   繁体   English

如何将新的 git 挂钩安装到所有现有(克隆)存储库?

[英]How to install new git hook to all existing (cloned) repositories?

I have ~50 repositories on my machine.我的机器上有大约 50 个存储库。 I would like to install newly created git hook to all of them.我想为所有这些安装新创建的 git 挂钩。

I've already created .git-templates folder and put hooks there, then git config --global init.templatedir ~/.git-templates - it works for new repositories but what about existing ones?我已经创建了.git-templates文件夹并将钩子放在那里,然后git config --global init.templatedir ~/.git-templates - 它适用于新的存储库,但是现有的存储库呢?

I you use a recent version of git (>= 2.9), then I would recommend using the core.hooksPath variable.我使用的是最新版本的git (>= 2.9),那么我建议使用core.hooksPath变量。

Then you can create a separate directory where you put all your hooks, eg /var/myhooks , then然后你可以创建一个单独的目录来放置所有的钩子,例如/var/myhooks ,然后

git config --global core.hooksPath /var/myhooks

would make this directory the default hooks directory for all your repositories at once.会立即将此目录设为所有存储库的默认挂钩目录。 As a side effect, all hooks under .git/hooks in each repository will be ignored.作为副作用,每个存储库中.git/hooks下的所有钩子都将被忽略。 But inside a specific repository, you could then override this again with its own hooks directory:但是在特定的存储库中,您可以使用自己的 hooks 目录再次覆盖它:

git config core.hooksPath .git/hooks

Just do it.去做就对了。 Find all the repositories and copy the hooks.找到所有存储库并复制挂钩。 Here's a sample (this might find more than you think, it's why the actual copy gets echoed not executed, don't just c&p the echoed commands if you might have ' 's in your pathnames):这是一个示例(这可能比您想象的要多,这就是实际副本被回显而不执行的原因,如果您的路径名中可能有' ,请不要只 c&p 回显命令):

find ~ -name HEAD -execdir test -f config -a -d objects -a -d refs \; \
        -execdir mkdir -p hooks \; \
        -printf "cp -a ~/.git-templates/hooks '%h'/hooks\\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM