签出分支时可以使git自动更新子模块吗? [英] Can I make git automatically update submodules when checking out a branch?

查看:126
本文介绍了签出分支时可以使git自动更新子模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有一些子模块的git存储库,这些子模块在不同的分支上具有不同的修订版.当我切换分支时,我得到:

I'm working on a git repository with some submodules, which have different revisions on different branches. When I switch branches, I get a:

M          path/to/subrepo
Switched to branch 'another-branch'

消息.然后,我手动执行以下操作:

message. I then manually do:

git submodule update --recursive

然后消失了.

我尝试在.git/hooks/post-checkout中编写一个git钩子:

I tried writing a git hook, in .git/hooks/post-checkout:

#!/bin/bash

exec git submodules update --recursive

但是这似乎没有任何作用.我试图从挂机后行添加exec echo hello-似乎也不起作用.

but this doesn't seem to do anything. I tried to add an exec echo hello from post-hook line - doesn't seem to work either.

我的问题:默认情况下,我可以配置git以便分支检出本身也更新子模块吗?

My question: Can I configure git so that the branch checkout itself will also update the submodules, by default?

推荐答案

我尝试在.git/hooks/post-checkout中编写一个git钩子:

I tried writing a git hook, in .git/hooks/post-checkout:

请检查您是否正确拼写了钩子的名称,并且钩子上有可执行标志( git 应该向您发出警告).

Check that you've spelled the name of the hook correctly and that the hook has the executable flag (git should give you a warning if it doesn't).

您发布的脚本中有一个错字:子模块的末尾不应有 s .也许这只是您发布的内容中的一个错误,但是如果它在您的实际钩子中,则在该钩子运行时您应该会看到一条错误消息,并且您实际上并没有(并且您的 echo 不会)无效)表明该挂钩根本没有运行.

You've got a typo in the script you posted: there should be no s at the end of submodule. Maybe that's just an error in what you posted, but if it's in your actual hook you should see an error message when the hook runs, and the fact that you don't (and also that your echo doesn't work) suggests that the hook isn't running at all.

如果您没有上面提到的错字,并且钩子中的 echo 语句可以正常工作,那么您看不到也就不足为奇了> git submodule update 行执行任何操作-如果项目中的子模块已经与分支中指定的提交匹配,则该命令不会提供任何输出.您唯一会看到输出的是是否确实有一个子模块需要实际更新.还请记住, git子模块更新 没有获得子模块的最新版本,它会您已经在项目中提交的项目.

If you don't have the typo mentioned above and if echo statements in your hook do work, then it's not surprising that you don't see your git submodule update line doing anything — that command doesn't give any output if the submodules in your project already match the commits that are specified in the branch. The only time you'll see output is if there's a submodule that actually needs to be updated. Also remember that git submodule update doesn't get the latest versions of the submodules, it gets the ones that you've committed in your project.

通常,您有一个正确的主意:您可以在 .git/hooks/中添加一个名为 post-checkout 的钩子,并且该钩子应在任何时候运行您已成功 git checkout 某个分支.

In general, you've got the right idea: you can definitely add a hook called post-checkout to .git/hooks/, and it should run whenever you successfully git checkout some branch.

这篇关于签出分支时可以使git自动更新子模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆