如何在 git 中的 fetch 或 pull 命令之后立即执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5623208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:36:08  来源:igfitidea点击:

How to execute a command right after a fetch or pull command in git?

gitghcgithooks

提问by fuz

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-allthat, when executed, updates all the dependent repositories.

我克隆了 GHC(格拉斯哥 Haskell 编译器)存储库。为了构建编译器,您需要几个库,它们都可以作为 git 存储库使用。为了简化这些操作,GHC 黑客包含了一个脚本sync-all,该脚本在执行时会更新所有依赖的存储库。

Now to my question: How can I make git execute ./sync-all pullafter I did a git pullautomatically? I heard something about using hooks, but I don't really know, what I have to do.

现在我的问题是:如何./sync-all pull在我git pull自动执行之后让 git 执行?我听说了一些关于使用钩子的事情,但我真的不知道,我必须做什么。

采纳答案by VonC

If you need to execute a script after a git pullon the client side (your machine), then a there is no reliable solution.

如果您需要git pull在客户端(您的机器)之后执行脚本,则没有可靠的解决方案。

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)

原始答案 (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-receiveor post-updatehook is generally used for this kind of task, executed after each push.
The difference between the post-receiveand post-updatehooks is in the arguments: a post-receivewill get both the old and the new values of all the refs in addition to their names.

一个post-receivepost-update钩子一般用于这种任务,在每次推送后执行。post-receivepost-update钩子
之间的区别在于参数:a将获得所有引用的旧值和新值,以及它们的名称。post-receive

The missing piece is: where that hook executes itself?

缺失的部分是:该钩子在哪里执行?

The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longairsheds some light on this:

来自(一位伟大的)SO 贡献者Mark Longair的博客文章“ Missing git hooks documentation对此有所了解:

the current working directory will be the git directory.

  • So, if this is a bare repository called “/src/git/test.git/”, that will be the current working directory – if this is a non-bare repository and the top level of the working tree is “/home/mark/test/” then the current working directory will be “/home/mark/test/.git/

Chris Johnsendetails in his SO answer: "If –-git-diror GIT_DIRare specified but none of -–work-tree, GIT_WORK_TREEand core.worktreeis specified, the current working directory is regarded as the top directory of your working tree."

In other words, your working tree will also be the current directory (the .gitdirectory), which almost certainly isn't what you want.

当前工作目录将是 git 目录

  • 所以,如果这是一个名为“ /src/git/test.git/”的裸仓库,那将是当前工作目录——如果这是一个非裸仓库并且工作树的顶层是“ /home/mark/test/”,那么当前工作目录将是“ /home/mark/test/.git/

Chris Johnsen在他的SO 回答中详细说明:“如果指定了–-git-dirGIT_DIR,但没有指定-–work-tree,GIT_WORK_TREEcore.worktree,则当前工作目录被视为工作树的顶级目录。

换句话说,您的工作树也将是当前目录(.git目录),这几乎肯定不是您想要的

Make sure your hook/post-receivescript, once created and made executable, sets the GIT_WORK_TREEat the right path, in order for your ./sync-all pullto be executed in the right directory (i.e. not "xxx.git" one).

确保您的hook/post-receive脚本在创建并执行后,将其设置GIT_WORK_TREE在正确的路径,以便您./sync-all pull在正确的目录中执行(即不是“ xxx.git”)。

回答by nullability

When you run git pull, git actually does a fetch followed by a merge. This means you can use the post-mergehook to execute a script when a pull is completed.

当您运行 时git pull,git 实际上会先进行一次 fetch,然后再进行一次合并。这意味着您可以post-merge在拉取完成时使用钩子执行脚本。

To set this up you just need to create an executable script in your repository's .git/hooks/directory called post-merge.

要进行设置,您只需要在存储库.git/hooks/目录中创建一个名为post-merge.

Note that this script will not be run if the merge fails due to conflicts.

请注意,如果合并因冲突而失败,则不会运行此脚本。