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

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


当前回答

正如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的未知选项。

其他回答

亨里克走在正确的轨道上。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

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

windows 2.6.3的Git:

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

我编写了这个简单的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 

如果是第一次签出回购,则需要使用--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)