Git 挂钩管理
我们在项目中使用自定义编写的 Git 挂钩。
Hooks 存储在项目的存储库中,当它们发生更改时,要获取新版本,每个用户必须将它们手动复制到他的 .git/hooks 目录中。这是比较不方便的。
改进这一点的一种方法是使 .git/hooks 成为工作树的符号链接。但这意味着每个分支(甚至是正在开发的用户本地功能分支)应该具有最新版本的钩子。这也不方便。
你会如何解决这个问题?
We use custom-written Git hooks in our project.
Hooks are stored in a project's repository, and, when they do change, to get a new version each user must copy them manually in his .git/hooks directory. This is rather inconvenient.
One way to improve this is to make .git/hooks a symlink into worktree. But this would imply that each branch (even user's local feature branches under development) should have the most current version of hooks. This is not convenient as well.
How would you solve the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
维护一个单独的钩子存储库和符号链接。
我同意,如果 Git 有一个用于传播 hooks 脚本的内置机制就好了,但它没有。
Maintain a separate repository of your hooks and symlink into that.
I agree, it'd be nice if Git had a built-in mechanism for propagating hooks scripts but it doesn't.
http://benjamin-meyer.blogspot .com/2010/06/managing-project-user-and-global-git.html 似乎是一种自动化钩子符号链接的便捷方法,以促进全局、每个用户和每个项目的钩子。
http://benjamin-meyer.blogspot.com/2010/06/managing-project-user-and-global-git.html appears to be a convenient way to automate hook symlinking, to facilitate global, per user, and per project hooks.
在 Git 2.9 中,您可以使用 core.hooksPath 配置指向自定义钩子目录。
Git 2.9 发行说明 - https://github.com/blog /2188-git-2-9-has-been-released
提及
core.hooksPath
的文档 - https://git-scm.com/docs/githooks注意 - 这个问题可能与 Git hook 脚本可以与存储库一起管理吗?
In Git 2.9, you can point to a custom hooks directory using the
core.hooksPath
configuration.Git 2.9 Release Notes - https://github.com/blog/2188-git-2-9-has-been-released
Documentation mentioning
core.hooksPath
- https://git-scm.com/docs/githooksNote - this question is a possible duplicate of Can Git hook scripts be managed along with the repository?
pre-commit 有很多看起来很有前途的功能
源代码此处
pre-commit has a bunch of features that look promising
Source code here
对于基于 NodeJS 的项目,我建议您查看 ghooks。
For NodeJS-based projects, I suggest you taking a look at ghooks.
您可以将存储库的
.git
目录设为 git 存储库,然后向其中添加钩子和其他配置,添加objects
、refs
等. 目录和文件,例如index
到其.gitignore
。不仅如此,您还可以设置一个接收后挂钩来从其来源更新元存储。一旦设置了初始配置,您就可以让它引入更新,而无需任何额外的工作。我正在研究这个细节,比如忽略列表中应该包含哪些内容;我将结果保存在 此存储库 的分支中。
You could make the repository's
.git
directory a git repository, and just add hooks and other config to it, adding theobjects
,refs
, etc. directories and files likeindex
to its.gitignore
. Not only that, but you could set up a post-receive hook to update the metarepo from its origin. Once you had the initial configuration set up, you could have it pull in updates without any additional effort.I'm working out the details of this, like what should go in the ignore list; I'm keeping the results in branches of this repo.
我们将 .git/hooks 创建为工作树的符号链接。
对于极少数情况下,当有人需要提交钩子将拒绝的文件时,我们使用 git commit --no-verify
We made .git/hooks a symlink into the working tree.
For those rare occasions when someone needs to commit files that the hooks will reject, we use
git commit --no-verify
命令 git config core.hooksPath $CUSTOM_GIT_HOOKS_PATH 会将您的钩子路径设置为所需的文件夹; 您可以为多个 git 进行配置,并为所有这些 git 使用相同的挂钩脚本
如果您想要全局挂钩配置,
git config --global core.hooksPath $CUSTOM_GIT_HOOKS_PATH
用于在 VSCode 中管理 GitHooks,您可以尝试我的扩展
GitHooks
GitHooks VSCode 市场链接
Command
git config core.hooksPath $CUSTOM_GIT_HOOKS_PATH
will set your hooks path to the required folder; you can configure this for multiple gits and use the same hook scripts for all of themif you want global hooks configuration
git config --global core.hooksPath $CUSTOM_GIT_HOOKS_PATH
for managing your GitHooks inside VSCode you can try out my extension
GitHooks
GitHooks VSCode Marketplace Link