我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。

如何获取所有git子模块的最新更改?


当前回答

编辑:

philfreo在评论中指出,需要最新版本。如果有任何嵌套子模块需要处于最新版本:

git submodule foreach --recursive git pull

-----下面的过期评论-----

这不是官方的做法吗?

git submodule update --init

我每次都用它。到目前为止没有问题。

编辑:

我刚刚发现你可以使用:

git submodule foreach --recursive git submodule update --init 

这也会递归地拉取所有子模块,即依赖关系。

其他回答

亨里克走在正确的轨道上。git子模块foreach命令可以执行任意shell脚本。最新的两个选择可能是:

git submodule foreach git pull origin master

and:

git submodule foreach /path/to/some/cool/script.sh

这将遍历所有初始化的子模块并运行给定的命令。

编辑:

philfreo在评论中指出,需要最新版本。如果有任何嵌套子模块需要处于最新版本:

git submodule foreach --recursive git pull

-----下面的过期评论-----

这不是官方的做法吗?

git submodule update --init

我每次都用它。到目前为止没有问题。

编辑:

我刚刚发现你可以使用:

git submodule foreach --recursive git submodule update --init 

这也会递归地拉取所有子模块,即依赖关系。

以下内容在Windows上适用。

git submodule init
git submodule update

对我来说,git2.24.03,更新到.gitmodules中定义的远程分支的最新提交。

git子模块更新--递归--init

git子模块更新--递归--远程

git版本2.24.3(Apple git-128)

请注意:有人说过git pull-递归子模块与git子模块update-递归-远程相同。但根据我的测试,gitpull-recurse子模块可能不会更新为.gitmodules中定义的远程分支的最新提交。

我经常使用这个命令,到目前为止,它仍然有效。

git pull
git submodule foreach --recursive git checkout master
git submodule foreach --recursive git pull

希望能快点。