我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
当前回答
对我来说,git2.24.03,更新到.gitmodules中定义的远程分支的最新提交。
git子模块更新--递归--init
git子模块更新--递归--远程
git版本2.24.3(Apple git-128)
请注意:有人说过git pull-递归子模块与git子模块update-递归-远程相同。但根据我的测试,gitpull-recurse子模块可能不会更新为.gitmodules中定义的远程分支的最新提交。
其他回答
亨里克走在正确的轨道上。git子模块foreach命令可以执行任意shell脚本。最新的两个选择可能是:
git submodule foreach git pull origin master
and:
git submodule foreach /path/to/some/cool/script.sh
这将遍历所有初始化的子模块并运行给定的命令。
如果是第一次签出回购,则需要使用--init first:
git submodule update --init --recursive
对于git1.8.2或更高版本,添加了选项--remote以支持更新到远程分支的最新提示:
git submodule update --recursive --remote
这还有一个额外的好处,那就是尊重.gitmodules或.git/config文件中指定的任何“非默认”分支(如果您碰巧有,默认值是origin/master,在这种情况下,这里的一些其他答案也会起作用)。
对于git 1.7.3或更高版本,您可以使用(但以下关于更新仍然适用的问题):
git submodule update --recursive
or:
git pull --recurse-submodules
如果您希望将子模块拉到最新的提交,而不是repo指向的当前提交。
详见git子模块(1)
你现在需要做的只是一个简单的git结账
只需确保通过以下全局配置启用它:git-config--global submodule.recure true
在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目录中,最适合我。
这将拉动所有最新的子模块。
编辑:
philfreo在评论中指出,需要最新版本。如果有任何嵌套子模块需要处于最新版本:
git submodule foreach --recursive git pull
-----下面的过期评论-----
这不是官方的做法吗?
git submodule update --init
我每次都用它。到目前为止没有问题。
编辑:
我刚刚发现你可以使用:
git submodule foreach --recursive git submodule update --init
这也会递归地拉取所有子模块,即依赖关系。