Git 挂钩脚本可以与存储库一起管理吗?
我们想要制作一些我们都可以共享的基本钩子脚本——用于预格式化提交消息之类的事情。 Git 有钩子脚本,通常存储在
下。 但是,当人们进行克隆时,这些脚本不会传播,并且它们不受版本控制。
有没有一个好的方法可以帮助大家获得正确的hook脚本呢? 我可以让这些钩子脚本指向我的存储库中的版本控制脚本吗?
We'd like to make a few basic hook scripts that we can all share -- for things like pre-formatting commit messages. Git has hook scripts for that that are normally stored under <project>/.git/hooks/
. However, those scripts are not propagated when people do a clone and they are not version controlled.
Is there a good way to help everyone get the right hook scripts? Can I just make those hook scripts point to version controlled scripts in my repo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
在 Git 2.9 中,
配置选项
core.hooksPath
< /a> 指定自定义挂钩目录。将您的钩子移至存储库中的
hooks
跟踪目录。 然后,配置存储库的每个实例以使用跟踪的hooks
而不是$GIT_DIR/hooks
:通常,路径可以是绝对路径,或相对于运行钩子的目录(通常是工作树根;请参阅
man 的描述部分githooks
)。In Git 2.9, the
configuration option
core.hooksPath
specifies a custom hooks directory.Move your hooks to a
hooks
tracked directory in your repository. Then, configure each instance of the repository to use the trackedhooks
instead of$GIT_DIR/hooks
:In general, the path may be absolute, or relative to the directory where the hooks are run (usually the working tree root; see DESCRIPTION section of
man githooks
).理论上,您可以在项目目录中创建一个包含所有脚本的
hooks
目录(或您喜欢的任何名称),然后在.git/hooks
中对它们进行符号链接。 当然,每个克隆存储库的人都必须设置这些符号链接(尽管您可能会非常喜欢并拥有一个克隆程序可以运行以半自动设置它们的部署脚本)。要在 *nix 上进行符号链接,您需要做的就是:
如果您准备好覆盖 .git/hooks 中的内容,请使用 ln -sf
Theoretically, you could create a
hooks
directory (or whatever name you prefer) in your project directory with all the scripts, and then symlink them in.git/hooks
. Of course, each person who cloned the repo would have to set up these symlinks (although you could get really fancy and have a deploy script that the cloner could run to set them up semi-automatically).To do the symlink on *nix, all you need to do is:
use
ln -sf
if you're ready to overwrite what's in.git/hooks
对于 Node.js 用户,一个简单的解决方案是更新 package.json 和
预安装 将在之前运行
并重定向 Git 以在 .\hooks (或您选择的任何名称)目录中查找挂钩。 该目录应在文件名(减去 .sample)和结构方面模仿 .\.git\hooks。
想象一下 Maven 和其他构建工具将具有相当于预安装的功能。
它还应该适用于所有平台。
如果您需要更多信息,请参阅与团队共享 Git hook 的两种方法。
For Node.js users a simple solution is to update package.json with
The preinstall will run before
and redirects Git to look for hooks inside the .\hooks (or whatever name you choose) directory. This directory should mimic .\.git\hooks in terms of file name (minus the .sample) and structure.
Imagine Maven and other build tools will have an equivalent to preinstall.
It should also work across all platforms.
If you need any more information, see Two ways to share Git hooks with your team.
我想将几个答案合并为一个。 假设您位于
project/
目录中:设置自定义挂钩
创建
.githooks
目录并将挂钩放入其中。 (有关示例,请参阅.git/hooks
)创建指向目录 1:
在
Makefile
中创建以下规则:²启用自定义挂钩
每个开发人员都应该查看这些自定义挂钩后明确启用它们。 在自述文件中添加一条指令,如下所示:
I wanted to merge several answers into one. Assuming you are in your
project/
directory:Setup your custom hooks
Create
.githooks
directory and place your hooks in it. (See.git/hooks
for examples)Create a
.gitconfig
file that points the directory ¹:Create the following rule in your
Makefile
: ²Enable your custom hooks
Every developer should explicitly enable these custom hooks after reviewing them. Add a directive to your README, something like that:
如果您的项目是 JavaScript 项目并且使用
npm
作为包管理器,则可以使用 shared-git-hooks 在npm install
上强制执行 Git 挂钩。完全披露:我写了这个包
If your project is a JavaScript project and you use
npm
as the package manager, you can use shared-git-hooks to enforce Git hooks onnpm install
.Full disclosure: I wrote this package
大多数现代编程语言,或者更确切地说是它们的构建工具,都支持插件来管理 Git 挂钩。 这意味着您需要做的就是配置 package.json、pom.xml 等文件,团队中的任何人都别无选择,只能遵守,除非他们更改构建文件。
该插件将为您添加内容到 .git 目录。
示例:
Git Build Hook Maven 插件< /em>
githook-maven-plugin
git-hooks-js
Most of the modern programming languages, or rather their build tools, support plugins to manage Git hooks. That means all you need to do is configure your package.json, pom.xml, etc. files, and anyone in your team will have no option but to comply unless they change the build file.
The plugin will add content to .git directory for you.
Examples:
Git Build Hook Maven Plugin
githook-maven-plugin
git-hooks-js
使用 git-hooks。 它将 .git/hooks 调用路由到项目目录 githooks 下的脚本中。
还有很多功能可以让您最大限度地减少各处的复制和符号链接挂钩。
Use git-hooks. It routes
.git/hooks
invoke into scripts under the project directory,githooks
.There are also a lot of features to enable you to minimize copy and symlink hook all over the place.
我们正在使用具有构建前和构建后事件的 Visual Studio 解决方案(以及项目)。 我正在添加一个名为“GitHookDeployer”的附加项目。 项目在构建后事件中自行修改文件。 该文件设置为复制到构建目录。 因此,该项目每次都会构建并且永远不会被跳过。 在构建事件中,它还确保所有 git hook 都就位。
请注意,这不是一个通用的解决方案,因为有些项目当然不需要构建任何东西。
We are using Visual Studio solutions (and thus projects) which have pre and post build events. I'm adding an additional project named 'GitHookDeployer'. The project self modifies a file in the post build event. That file is set to copy to the build directory. Thus the project is build every time and is never skipped. In the build event, it also makes sure that all git hooks are in place.
Note that this is not a general solution, as some projects, of course, have nothing to build.
您可以使用托管解决方案进行预提交挂钩管理,例如 预提交。
或者是服务器端 git-hooks 的集中式解决方案,例如 Datree.io。
它具有内置策略,例如:
它不会取代您的所有钩子,但它可能会帮助您的开发人员使用最明显的钩子,而无需在每个钩子上安装钩子的配置地狱。
开发人员计算机/仓库。
免责声明:我是 Datrees 创始人之一
You could use a managed solution for pre-commit hook management like pre-commit.
Or a centralized solution for server-side git-hooks like Datree.io.
It has built-in policies like:
It won't replace all of your hooks, but it might help your developers with the most obvious ones without the configuration hell of installing the hooks on every
developers computer/repo.
Disclaimer: I am one of Datrees founders
理想情况下,如果您遵循示例文件,则挂钩是用 Bash 编写的。 但您可以用任何可用的语言编写它,只需确保它具有可执行标志即可。
因此,您可以编写Python或Go代码来实现您的目标,并将其放在hooks文件夹下。 它可以工作,但不会与存储库一起管理。
两个选项
a) 多脚本
您可以在帮助中编写挂钩,并在挂钩中添加一小段代码,以调用您的完美脚本,如下所示:
b) 单个脚本
一种更酷的选择是仅添加一个脚本来规则所有这些,而不是多个脚本。 因此,您创建一个 hooks/mysuperhook.go 文件并将您想要的每个钩子指向它。
该参数将向您的脚本提供触发了哪个钩子,您可以在代码中区分它。 为什么? 例如,有时您可能希望对提交和推送运行相同的检查。
然后?
然后,您可能需要更多功能,例如:
这可以更简单吗?
是的,有多种工具可以帮助您管理 Git 挂钩。 它们中的每一个都是为了从不同的角度解决问题而量身定制的,您可能需要了解所有这些,才能找到最适合您或您的团队的一个。 GitHooks.com 提供了大量有关挂钩的阅读材料,以及当今可用的多种工具。
截至今天,那里列出了 21 个项目,它们采用不同的策略来管理 Git 挂钩。 有些仅针对单个钩子,有些针对特定语言,等等。
其中一个工具是我编写的,作为开源项目免费提供,名为 hooks4git 。 它是用 Python 编写的(因为我喜欢它),但其想法是在一个名为 .hooks4git.ini 的配置文件中处理上面列出的所有项目,该文件位于您的存储库中并可以调用任何脚本您想用任何语言打电话。
使用 Git hooks 绝对是非常棒的,但它们提供的方式通常只会让人们远离它。
Ideally, hooks are written in Bash, if you follow the sample files. But you can write it in any language available, and just make sure it has the executable flag.
So, you can write a Python or Go code to achieve your goals, and place it under the hooks folder. It will work, but it will not be managed along with the repository.
Two Options
a) Multi Scripts
You can code your hooks inside your help, and add a small fragment of code to hooks, to call your perfect script, like this:
b) Single Script
A cooler option is to add just one script to rule them all, instead of several ones. So, you create a hooks/mysuperhook.go file and point every hook you want to have to it.
The parameter will provide your script which hook was triggered, and you can differentiate it inside your code. Why? Sometimes you might want to run the same check for commit and push, for instance.
And then?
Then, you might want to have further functionalities, like:
Can this be simpler?
Yes, there are several tools to help you manage Git hooks. Each of them is tailored to tackle the problem from a different perspective, and you might need to understand all of them to get the one that is best for you or your team. GitHooks.com offers a lot of reading about hooking, and several tools available today.
As of today, there are 21 projects listed there with different strategies to manage Git hooks. Some only do it for a single hook, some for a specific language, and so on.
One of those tools, written by me and offered for free as an open-source project, is called hooks4git. It is written in Python (because I like it), but the idea is to handle all items listed above in a single configuration file called .hooks4git.ini, which lives inside your repository and can call any script you want to call, in any language.
Using Git hooks is absolutely fantastic, but the way they are offered usually only gets people away from it.
对于 Gradle 用户,
我发现这些脚本对于 Gradle 项目非常有用。
build.gradle
apply from: rootProject.file('gradle/install-git-hooks.gradle')
gradle/install-git-hooks.gradle
预提交
For Gradle users
I found these scripts very useful for Gradle projects.
build.gradle
apply from: rootProject.file('gradle/install-git-hooks.gradle')
gradle/install-git-hooks.gradle
pre-commit
受到 a3765910 的 Gradle answer 的启发,但经过修改以运行每个构建。
将以下内容添加到应用程序的 build.gradle 中(假设您已经创建了要保留在源代码管理中的 githooks/pre-commit 文件):
Inspired by a3765910's answer for Gradle but modified to run every build.
Add the following to your app's build.gradle (assuming you already created the githooks/pre-commit file you want to keep in source control):
您可以将您的 hooks 文件夹创建为另一个 Git 存储库,并将其作为子模块链接...
我想只有当您有很多成员并且定期更改挂钩时才值得这样做。
You can make your hooks folder another Git repository and link it as a submodule...
I guess it is worth it only if you have a lot of members and hooks changed regularly.
我目前正在我们的代码库中处理此问题,并且遇到了一个名为
husky
的库,它简化了如何在团队中使用和共享 GitHub Hooks。 我强烈建议对此进行调查。I'm currently working on this in our codebase and I came across a library called
husky
which simplifies how to use and share GitHub Hooks across your team. I highly recommend looking into that.要版本化您的 hooks 目录(假设它存储在
hooks/
中),请在工作副本根目录中创建一个.gitconfig
文件,其中包含以下内容:它将覆盖
。 git/config
.To version your hooks directory (assuming it's stored in
hooks/
), create a.gitconfig
file at the working copy root with the following contents:It will override
.git/config
.