如何在 git 中执行 fetch 或 pull 命令后立即执行命令?
我克隆了 GHC(格拉斯哥 Haskell 编译器)存储库。为了构建编译器,您需要几个库,所有这些库也都可以作为 git 存储库提供。为了方便人们的生活,GHC 黑客添加了一个脚本 sync-all
,该脚本在执行时会更新所有依赖的存储库。
现在我的问题是:如何让 git 在自动执行 git pull
后执行 ./sync-all pull
?我听说过一些关于使用钩子的事情,但我真的不知道我必须做什么。
I cloned the GHC (Glasgow Haskell Compiler) repository. In order to build the compiler, you need several libraries, all of them are available as git repositories too. In order to ease ones live, the GHC hackers included a script sync-all
that, when executed, updates all the dependent repositories.
Now to my question: How can I make git execute ./sync-all pull
after I did a git pull
automatically? I heard something about using hooks, but I don't really know, what I have to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要在客户端(您的计算机)上执行 git pull 后执行脚本,那么没有可靠的解决方案。
合并后客户端挂钩 (此处提到)不会被每次拉动触发(例如,不在拉动变基中,不在快进合并的情况下),
因此包装两个命令的别名仍然是一个可能的解决方法。
原始答案(2011)
如果您需要在服务器端,您可以在每次推送到裸存储库(在服务器上)后检查裸存储库并从工作树(在服务器上)中提取:
A < code>post-receive 或
post-update
钩子通常用于此类任务,在每次推送后执行。post-receive 和 post-update 挂钩之间的区别在于参数:
post-receive
将同时获取旧的和新的除了名称之外,还有所有引用的值。缺少的部分是:该钩子在哪里执行自身?
博客文章“缺少 git hooks 文档" 来自(一位伟大的)SO 贡献者 Mark Longair 对此做了一些说明:
确保您的
hook/post-receive
脚本在创建并可执行后,将GIT_WORK_TREE
设置在正确的路径,以便您的./sync-all pull
在正确的目录中执行(即不是“xxx.git
”目录)。If you need to execute a script after a
git pull
on the client side (your machine), then a there is no reliable solution.The post-merge client-side hook (mentioned here) won't be triggered by every pull (for instance, not in a pull rebase, not in case of fast-forward merge)
So an alias wrapping both commands remains a possible workaround.
Original answer (2011)
If you need that on the server side, where you would checkout the bare repo and pull from a working tree (on the server) after each push to a bare repo (on the server):
A
post-receive
orpost-update
hook is generally used for this kind of task, executed after each push.The difference between the post-receive and post-update hooks is in the arguments: a
post-receive
will get both the old and the new values of all the refs in addition to their names.The missing piece is: where that hook executes itself?
The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longair sheds some light on this:
Make sure your
hook/post-receive
script, once created and made executable, sets theGIT_WORK_TREE
at the right path, in order for your./sync-all pull
to be executed in the right directory (i.e. not "xxx.git
" one).当您运行 git pull 时,git 实际上会先进行提取,然后进行合并。这意味着您可以使用
post-merge
挂钩在拉取完成后执行脚本。要进行此设置,您只需在存储库的
.git/hooks/
目录中创建一个名为post-merge
的可执行脚本。请注意,如果合并由于冲突而失败,则该脚本将不会运行。
When you run
git pull
, git actually does a fetch followed by a merge. This means you can use thepost-merge
hook to execute a script when a pull is completed.To set this up you just need to create an executable script in your repository's
.git/hooks/
directory calledpost-merge
.Note that this script will not be run if the merge fails due to conflicts.