我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。
如何获取所有git子模块的最新更改?
当前回答
上面的答案很好,不过我们使用了git钩子来简化这一过程,但事实证明,在git2.14中,您可以将git-config子模块.recure设置为true,以便在您拉到git存储库时更新子模块。
如果所有子模块都在分支上,这会产生一个副作用,即推动所有子模块的更改,但如果您已经需要这种行为,这可以完成任务。
可通过以下方式完成:
git config submodule.recurse true
其他回答
亨里克走在正确的轨道上。git子模块foreach命令可以执行任意shell脚本。最新的两个选择可能是:
git submodule foreach git pull origin master
and:
git submodule foreach /path/to/some/cool/script.sh
这将遍历所有初始化的子模块并运行给定的命令。
我通过修改上面gahooa的回答做到了这一点:
将其与git〔alias〕集成。。。
如果您的父项目在.gitmodules中有类似的内容:
[submodule "opt/submodules/solarized"]
path = opt/submodules/solarized
url = git@github.com:altercation/solarized.git
[submodule "opt/submodules/intellij-colors-solarized"]
path = opt/submodules/intellij-colors-solarized
url = git@github.com:jkaving/intellij-colors-solarized.git
在.gitconfig中添加类似的内容
[alias]
updatesubs = "!sh -c \"git submodule init && git submodule update && git submodule status\" "
然后,要更新子模块,请运行:
git updatesubs
我在环境设置repo中有一个示例。
编辑:
philfreo在评论中指出,需要最新版本。如果有任何嵌套子模块需要处于最新版本:
git submodule foreach --recursive git pull
-----下面的过期评论-----
这不是官方的做法吗?
git submodule update --init
我每次都用它。到目前为止没有问题。
编辑:
我刚刚发现你可以使用:
git submodule foreach --recursive git submodule update --init
这也会递归地拉取所有子模块,即依赖关系。
正如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的未知选项。
备注:不太容易,但可行,它有自己独特的优点。
若要只克隆存储库的HEAD修订版和所有子模块的HEAD(即签出“trunk”),那个么可以使用以下Lua脚本。有时,简单的命令git submodule update--init--recursive--remote--no fetch--depth=1会导致不可恢复的git错误。在这种情况下,需要清理.git/modules目录的子目录,并使用gitclone--separate gitdir命令手动克隆子模块。唯一的复杂性是在超级项目树中查找子模块的URL、.git目录的路径和子模块的路径。
备注:脚本仅针对https://github.com/boostorg/boost.git存储库。它的特点:托管在同一主机上的所有子模块和.gitmodules只包含相对的URL。
-- mkdir boost ; cd boost ; lua ../git-submodules-clone-HEAD.lua https://github.com/boostorg/boost.git .
local module_url = arg[1] or 'https://github.com/boostorg/boost.git'
local module = arg[2] or module_url:match('.+/([_%d%a]+)%.git')
local branch = arg[3] or 'master'
function execute(command)
print('# ' .. command)
return os.execute(command)
end
-- execute('rm -rf ' .. module)
if not execute('git clone --single-branch --branch master --depth=1 ' .. module_url .. ' ' .. module) then
io.stderr:write('can\'t clone repository from ' .. module_url .. ' to ' .. module .. '\n')
return 1
end
-- cd $module ; git submodule update --init --recursive --remote --no-fetch --depth=1
execute('mkdir -p ' .. module .. '/.git/modules')
assert(io.input(module .. '/.gitmodules'))
local lines = {}
for line in io.lines() do
table.insert(lines, line)
end
local submodule
local path
local submodule_url
for _, line in ipairs(lines) do
local submodule_ = line:match('^%[submodule %"([_%d%a]-)%"%]$')
if submodule_ then
submodule = submodule_
path = nil
submodule_url = nil
else
local path_ = line:match('^%s*path = (.+)$')
if path_ then
path = path_
else
submodule_url = line:match('^%s*url = (.+)$')
end
if submodule and path and submodule_url then
-- execute('rm -rf ' .. path)
local git_dir = module .. '/.git/modules/' .. path:match('^.-/(.+)$')
-- execute('rm -rf ' .. git_dir)
execute('mkdir -p $(dirname "' .. git_dir .. '")')
if not execute('git clone --depth=1 --single-branch --branch=' .. branch .. ' --separate-git-dir ' .. git_dir .. ' ' .. module_url .. '/' .. submodule_url .. ' ' .. module .. '/' .. path) then
io.stderr:write('can\'t clone submodule ' .. submodule .. '\n')
return 1
end
path = nil
submodule_url = nil
end
end
end