我们使用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

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

我不知道这是从哪个版本的git开始工作的,但这就是您正在搜索的:

git submodule update --recursive

我也使用git pull来更新根存储库:

git pull && git submodule update --recursive

由于您的子模块的默认分支可能不是master,这就是我如何自动化整个Git子模块的升级:

git submodule init
git submodule update
git submodule foreach 'git fetch origin; git checkout $(git rev-parse --abbrev-ref HEAD); git reset --hard origin/$(git rev-parse --abbrev-ref HEAD); git submodule update --recursive; git clean -dfx'

第一次

克隆和初始化子模块

git clone git@github.com:speedovation/kiwi-resources.git resources
git submodule init

Rest

在开发过程中,只需拉动并更新子模块

git pull --recurse-submodules  && git submodule update --recursive

将Git子模块更新为最新的提交源代码

git submodule foreach git pull origin master

首选方式如下

git submodule update --remote --merge

注意:最后两个命令具有相同的行为

在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目录中,最适合我。

这将拉动所有最新的子模块。