我们正在使用 git 子模块来管理几个依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的存储库,作为子模块引入依赖项目。在开发过程中,我们经常想去获取每个依赖子模块的最新版本。
如何获取所有 git 子模块的最新更改?
如果是第一次签出您需要先使用 --init
的存储库:
git submodule update --init --recursive
git 1.8.2 或更高版本,添加了选项 --remote
以支持更新到远程分支的最新提示:
git submodule update --recursive --remote
这具有尊重 .gitmodules
或 .git/config
文件中指定的任何“非默认”分支的额外好处(如果您碰巧有,默认为 origin/master,在这种情况下,这里的一些其他答案将作为出色地)。
对于 git 1.7.3 或更高版本,您可以使用(但以下关于更新仍然适用的问题):
git submodule update --recursive
或者:
git pull --recurse-submodules
如果您想将您的子模块拉到最新提交而不是当前提交,则 repo 指向。
有关详细信息,请参阅 git-submodule(1)
git pull --recurse-submodules --jobs=10
git 在 1.8.5 中首次学习的功能。
在 bug 修复之前,您第一次确实需要运行
git 子模块更新 --init --recursive
git pull --recurse-submodules
和 git submodule update --recursive
都不初始化新添加的子模块。要初始化它们,您需要运行 git submodule update --recursive --init
。引用 manual:如果子模块尚未初始化,而您只想使用存储在 .gitmodules 中的设置,则可以使用 --init 选项自动初始化子模块。
git submodule update --recursive --remote
添加一个提示,它还将子模块更新为远程最新版本,而不是存储的 SHA-1。
在初始化时运行以下命令:
git submodule update --init --recursive
从 git repo 目录中,最适合我。
这将提取所有最新的包括子模块。
解释
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
从 git repo 目录中,最适合我。
这将提取所有最新的包括子模块。
注意:这是从 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 子模块状态。输出行的第一个字符表明了这一点。如果修改了子存储库,您可能不想继续。对于列出的每个 repo,cd 进入它的目录并运行 git checkout master && git pull。检查错误。最后,我建议您向用户打印一个显示以指示子模块的当前状态——也许提示他们添加所有并提交?
我想提一下,这种风格并不是 git 子模块的真正设计目的。通常,您想说“LibraryX”的版本是“2.32”,并且会一直保持这种状态,直到我告诉它“升级”。
也就是说,从某种意义上说,您使用所描述的脚本正在做什么,但只是更自动。需要呵护!
更新 2:
如果你在 Windows 平台上,你可能想看看使用 Python 来实现脚本,因为它在这些领域非常有能力。如果您使用的是 unix/linux,那么我建议您只使用 bash 脚本。
需要任何说明吗?只需发表评论。
git config --global alias.pup '!git pull && git submodule init && git submodule update && git submodule status'
,然后将其用作 git pup
,无需任何脚本。
git submodule init
,这样一切才能开始正常工作。
亨里克走在正确的轨道上。 'foreach' 命令可以执行任意的 shell 脚本。提取最新版本的两个选项可能是,
git submodule foreach git pull origin master
和,
git submodule foreach /path/to/some/cool/script.sh
这将遍历所有初始化的子模块并运行给定的命令。
以下内容在 Windows 上对我有用。
git submodule init
git submodule update
第一次
克隆和初始化子模块
git clone git@github.com:speedovation/kiwi-resources.git resources
git submodule init
休息
在开发过程中只需拉取和更新子模块
git pull --recurse-submodules && git submodule update --recursive
将 Git 子模块更新为源上的最新提交
git submodule foreach git pull origin master
首选方式应该在下面
git submodule update --remote --merge
注意:最后两个命令具有相同的行为
git submodule update
成功了。现在我正在下载克隆第一步中缺少的子模块数据。谢谢你。我不擅长 git :C
编辑:
在评论中指出(由 philfreo )需要最新版本。如果有任何嵌套子模块需要在其最新版本中:
git submodule foreach --recursive git pull
-----下面的过时评论-----
这不是官方的做法吗?
git submodule update --init
我每次都用它。到目前为止没有问题。
编辑:
我刚刚发现你可以使用:
git submodule foreach --recursive git submodule update --init
这也将递归地提取所有子模块,即依赖项。
git submodule update --init --recursive
git submodule foreach --recursive git pull
由于您的子模块的默认分支可能是 not 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'
master
和 main
的子模块。谢谢!
对我来说,git 2.24.03 更新到 .gitmodules 中定义的远程分支的最新提交。
git submodule update --recursive --init
git submodule update --recursive --remote
git 版本 2.24.3 (Apple Git-128)
请注意:有人说git pull --recurse-submodules
与git submodule update --recursive --remote
相同。但从我的测试来看,git pull --recurse-submodules
可能无法更新为 .gitmodules 中定义的远程分支的最新提交。
我不知道这是哪个版本的 git,但这就是您要搜索的内容:
git submodule update --recursive
我也将它与 git pull
一起使用来更新根存储库:
git pull && git submodule update --recursive
上面的答案很好,但是我们使用 git-hooks 来简化它,但事实证明,在 git 2.14 中,您可以将 git config submodule.recurse
设置为 true 以使子模块在您拉取时更新到您的 git 存储库。
如果它们在分支上,这将产生推动所有子模块更改的副作用,但是如果您已经需要这种行为,这可以完成这项工作。
可以通过使用来完成:
git config submodule.recurse true
git submodule init
。
根据从远程提取每个子模块的“最新”代码的现有答案来澄清一些事情。
如果“最新”是指签入的子模块指针,那么一定要使用:
git submodule update --recursive
- or -
git pull --recurse-submodules --jobs=X
如果“最新”意味着最新的主要,那么这样的事情可以工作:
git submodule foreach "git checkout main && git pull"
不幸的是,这意味着没有“--jobs”选项,所以我们不能并行运行它。我见过的最接近并行运行的方法是使用 pfs python 代码。
我经常使用这个命令,到目前为止它有效。
git pull
git submodule foreach --recursive git checkout master
git submodule foreach --recursive git pull
希望这更快。
适用于 Windows 2.6.3 的 Git:
git submodule update --rebase --remote
从 repo 的顶层:
git submodule foreach git checkout develop
git submodule foreach git pull
这将切换所有分支以开发和拉最新
git submodule foreach git pull origin master
必须附加我想要获取的分支。除此之外,工作完美。
我通过调整 gahooa 的 answer above 来做到这一点:
将其与 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
我的 environment setup repo 中有一个 example。
您现在需要做的只是一个简单的 git checkout
只需确保通过此全局配置启用它:git config --global submodule.recurse true
这是从所有 git 存储库中提取的命令行,无论它们是否是子模块:
ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'
如果您在顶级 git 存储库中运行它,您可以将 "$ROOT"
替换为 .
。
备注:不是太简单的方法,但可行,它有自己独特的优点。
如果只想克隆存储库的 HEAD
修订版及其所有子模块的 HEAD
(即检查“主干”),则可以使用以下 Lua 脚本。有时简单的命令 git submodule update --init --recursive --remote --no-fetch --depth=1
会导致不可恢复的 git
错误。在这种情况下,需要清理 .git/modules
目录的子目录并使用 git clone --separate-git-dir
命令手动克隆子模块。唯一复杂的是找出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
我认为您必须编写一个脚本来执行此操作。老实说,我可能会安装 python 来执行此操作,以便您可以对每个目录使用 os.walk
到 cd
并发出适当的命令。使用 python 或其他脚本语言(批处理除外)可以让您轻松添加/删除子项目,而无需修改脚本。
git submodule update --recursive
。git submodule foreach "(git checkout master; git pull)&"
origin master
可能会产生意外结果。对某些人来说很明显,但对所有人来说可能不是。git submodule update --recursive
查看父存储库为每个子模块存储了哪个修订,然后在每个子模块中检查该修订。它确实 NOT 拉取每个子模块的最新提交。如果您打算将每个子模块从其原始存储库更新到最新版本,那么git submodule foreach git pull origin master
或git pull origin master --recurse-submodules
就是您想要的。只有这样,您才能在父 repo 中获得更新的子模块修订哈希值的未决更改。检查这些,你很好。