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

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


当前回答

我编写了这个简单的shell脚本,对我来说很好。

#!/bin/bash

#git init
git init

#git clone repo including submodules
git clone --recurse-submodules https://github.com/username/project.git

#change directory -repo
cd project

#update the remote ie tag/commits
git submodule update --remote

#add commit 
git commit -a -m "commit in submodule"

#git push 
git push -u origin 

其他回答

windows 2.6.3的Git:

git子模块更新--rebase--remote

以下内容在Windows上适用。

git submodule init
git submodule update

正如antitoxic的回答所指出的,一个简单的git子模块foreach——递归git pull就足够了。

弗朗西斯·培根(Francis Bacon)的回答指出,git pull-递归子模块可能不同。

您可以使用详细选项进行测试并查看发生了什么:

git pull -v --recurse-submodules

但为此,您需要Git 2.40(2023年第1季度)。

“git pull-v--recurse submodule”(man)试图将-v传递给底层git submodule update(man),后者不理解请求并弹出:这已经用git 2.40(2023年第1季度)纠正。

参见Sven Strickroth(csware)提交的6f65f84(2022年12月10日)。(由Junio C Hamano--gitster--在提交b3b9e5c中合并,2022年12月28日)

子模块:接受更新命令的-v签字人:Sven Strickroth

由于a56771a(“builtin/pull:尊重子模块中的冗余设置”,2018-01-25,Git v2.17.0-rc0--批次#3中列出的merge),“Git pull-v--递归子模块”(man)将-v传播到子模块命令,但因为后者命令不理解该选项,所以它会退出。教“git子模块更新”(man)接受修复选项。

不再“barfing”(即显示用法'gitsubmodule foreach[-quiet][--recursive][--]<command>'),因为-v是gitsubmodule的未知选项。

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

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

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

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

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

git版本2.24.3(Apple git-128)

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