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

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


当前回答

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

git submodule foreach git pull origin master

and:

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

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

其他回答

第一次

克隆和初始化子模块

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

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

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

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

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

git版本2.24.3(Apple git-128)

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

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

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

下面是从所有git存储库中提取的命令行,无论它们是否是子模块:

ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'

如果您在顶级git存储库中运行它,可以将“$ROOT”替换为。。

注:这是从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脚本。

需要澄清吗?只需发表评论。