我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
当前回答
编辑:
philfreo在评论中指出,需要最新版本。如果有任何嵌套子模块需要处于最新版本:
git submodule foreach --recursive git pull
-----下面的过期评论-----
这不是官方的做法吗?
git submodule update --init
我每次都用它。到目前为止没有问题。
编辑:
我刚刚发现你可以使用:
git submodule foreach --recursive git submodule update --init
这也会递归地拉取所有子模块,即依赖关系。
其他回答
下面是从所有git存储库中提取的命令行,无论它们是否是子模块:
ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'
如果您在顶级git存储库中运行它,可以将“$ROOT”替换为。。
我不知道这是从哪个版本的git开始工作的,但这就是您正在搜索的:
git submodule update --recursive
我也使用git pull来更新根存储库:
git pull && git submodule update --recursive
在init上运行以下命令:
git submodule update --init --recursive
在gitrepo目录中,最适合我。
这将拉动所有最新的子模块。
解释
git - the base command to perform any git command
submodule - Inspects, updates and manages submodules.
update - Update the registered submodules to match what the superproject
expects by cloning missing submodules and updating the working tree of the
submodules. The "updating" can be done in several ways depending on command
line options and the value of submodule.<name>.update configuration variable.
--init without the explicit init step if you do not intend to customize
any submodule locations.
--recursive is specified, this command will recurse into the registered
submodules, and update any nested submodules within.
之后,您可以运行:
git submodule update --recursive
在gitrepo目录中,最适合我。
这将拉动所有最新的子模块。
注:这是从2009年开始的,当时可能不错,但现在有更好的选择。
我们使用这个。它叫git pup:
#!/bin/bash
# Exists to fully update the git repo that you are sitting in...
git pull && git submodule init && git submodule update && git submodule status
只需将其放在合适的bin目录(/usr/local/bin)中。如果在Windows上,您可能需要修改语法以使其正常工作:)
更新:
作为对原作者关于插入所有子模块的所有HEAD的评论的回应,这是一个很好的问题。
我很确定git内部没有这个命令。为了做到这一点,您需要确定HEAD对于子模块的真正含义。这可能很简单,比如说master是最新的分支,等等。。。
接下来,创建一个简单的脚本,执行以下操作:
检查git子模块状态以查找“已修改”的存储库。输出行的第一个字符表示这一点。如果子回购被修改,您可能不想继续。对于列出的每个回购,将其cd到其目录中,并运行gitcheckout-master和gitpull。检查错误。最后,我建议您向用户打印一个显示,以指示子模块的当前状态——也许会提示他们添加全部并提交?
我想指出的是,这种风格并不是git子模块的设计初衷。通常,你想说“LibraryX”的版本是“2.32”,在我告诉它“升级”之前,它会一直这样。
从某种意义上说,这就是您使用所描述的脚本所做的事情,但只是更加自动。需要小心!
更新2:
如果您使用的是windows平台,您可能需要考虑使用Python来实现脚本,因为它在这些方面非常有用。如果您使用的是unix/linux,那么我建议您使用bash脚本。
需要澄清吗?只需发表评论。
上面的答案很好,不过我们使用了git钩子来简化这一过程,但事实证明,在git2.14中,您可以将git-config子模块.recure设置为true,以便在您拉到git存储库时更新子模块。
如果所有子模块都在分支上,这会产生一个副作用,即推动所有子模块的更改,但如果您已经需要这种行为,这可以完成任务。
可通过以下方式完成:
git config submodule.recurse true