我不知道从哪个版本的git开始工作,但这就是你要搜索的内容:
git submodule update --recursive
我用它 git pull 还要更新根存储库:
git pull
git pull && git submodule update --recursive
这是从所有git存储库中提取的命令行,无论它们是否是子模块:
ROOT=$(git rev-parse --show-toplevel 2> /dev/null) find "$ROOT" -name .git -type d -execdir git pull -v ';'
如果您在顶级git存储库中运行它,则可以替换 "$ROOT" 成 . 。
"$ROOT"
.
你现在需要做的就是简单 git checkout
git checkout
只需确保通过此全局配置启用它: git config --global submodule.recurse true
git config --global submodule.recurse true
对于 的 git 1.8.2 强> 或以上选项 --remote 添加以支持更新远程分支的最新提示:
--remote
git submodule update --recursive --remote
这具有额外的好处,即尊重在中指定的任何“非默认”分支 .gitmodules 要么 .git/config 文件(如果您碰巧有任何,默认是origin / master,在这种情况下,其他一些答案也适用)。
.gitmodules
.git/config
对于 的 git 1.7.3 强> 您可以使用或以上(但以下关于更新仍然适用的问题):
要么:
git pull --recurse-submodules
如果你想把你的子模块拉到repo指向的最新提交intead。
注意:如果那样的话 的 第一次 强> 你检查一下你需要使用的仓库 --init 第一:
--init
git submodule update --init --recursive
对于 的 年长的,git 1.6.1 强> 或以上你可以使用类似的东西(修改为适合):
git submodule foreach git pull origin master
看到 GIT-子模块(1) 详情
备注:不是太容易,但可行,它有自己独特的优点。
如果只想克隆 HEAD 仅修订存储库 HEAD 在其所有子模块(即结账“主干”)中,可以使用以下内容 LUA 脚本。有时简单命令 git submodule update --init --recursive --remote --no-fetch --depth=1 会导致无法恢复 git 错误。在这种情况下,需要清理子目录 .git/modules 手动使用目录和克隆子模块 git clone --separate-git-dir 命令。唯一的复杂性是找出答案 网址 ,路径 .git 子模块目录和超级项目树中子模块的路径。
HEAD
git submodule update --init --recursive --remote --no-fetch --depth=1
git
.git/modules
git clone --separate-git-dir
.git
备注:脚本仅针对测试 https://github.com/boostorg/boost.git 库。它的特点:所有子模块都托管在同一主机上 .gitmodules 只包含亲戚 网址 秒。
https://github.com/boostorg/boost.git
-- 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
从回购的顶层:
git submodule foreach git checkout develop git submodule foreach git pull
这将切换所有分支以开发和拉取最新
Git for windows 的 2.6.3 强> :
git submodule update --rebase --remote
我通过改编做到了这一点 gahooa 的 回答上面 :
将它与git集成 [alias] ...
[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
我有一个 例 在我的 环境设置回购 。
我想你必须写一个脚本才能做到这一点。说实话,我可能会安装python来做你可以使用的 os.walk 至 cd 到每个目录并发出适当的命令。除了批处理之外,使用python或其他一些脚本语言将允许您轻松添加/删除子项目而无需修改脚本。
os.walk
cd
亨里克走在正确的轨道上。 'foreach'命令可以执行任何任意shell脚本。拉最新的两种选择可能是,
和,
git submodule foreach /path/to/some/cool/script.sh
这将遍历所有 初始化 子模块并运行给定的命令。
因为可能发生了子模块的默认分支 的 不 强> master ,这就是我自动化完整Git子模块升级的方法:
master
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'
看着 http://lists.zerezo.com/git/msg674976.html 它引入了一个--track参数
以下在Windows上为我工作。
git submodule init git submodule update