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

git是否有内置命令来执行此操作?如果没有,Windows批处理文件或类似的文件怎么样呢?

有帮助吗?

解决方案

如果第一次你签出了一个仓库,你需要先使用 - 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
  

首次在1.7.3中学习的功能git。

但是这不会检查子模块中正确的提交(主存储库指向的提交)

要检查子模块中的正确提交,您应该在使用

后进行更新
git submodule update --recursive --remote

在init上运行以下命令:

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 是最新的分支等等......

在此之后,创建一个执行以下操作的简单脚本:

  1. 检查的“ git submodule status ”库。输出行的第一个字符表示这一点。如果修改了子仓库,您可能不想继续。
  2. 列出的每个仓库,cd进入它的目录并运行 git checkout master&amp;&amp; git pull 。检查错误。
  3. 最后,我建议您打印一个显示给用户以指示子模块的当前状态 - 可能会提示他们添加所有并提交?
  4. 我想提一下,这种风格并不是git子模块的设计用途。通常,您想要说“LibraryX”。是版本“2.32”并且会保持这种状态,直到我告诉它“升级”。

    从某种意义上说,就是你正在用所描述的脚本做什么,但更自动。需要小心!

    更新2:

    如果您使用的是Windows平台,则可能需要使用Python来实现脚本,因为它在这些方面非常强大。如果您使用的是unix / linux,那么我建议只使用一个bash脚本。

    需要澄清吗?只需发表评论。

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

修改

在评论中指出(通过 philfreo )需要最新版本。如果有任何嵌套子模块需要在其最新版本中:

git submodule foreach --recursive git pull

-----下面的过时评论-----

这不是官方的做法吗?

git submodule update --init

我每次都用它。到目前为止没问题。

修改

我刚发现你可以使用:

git submodule foreach --recursive git submodule update --init 

还将递归拉出所有子模块,即依赖。

由于可能发生子模块的默认分支是 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'

第一次

克隆和初始化子模块

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开始工作,但这就是你要搜索的内容:

git submodule update --recursive

我将它与 git pull 一起使用来更新根存储库:

git pull && git submodule update --recursive

请参阅 http://lists.zerezo.com/git/msg674976.html引入了--track参数

上面的答案很好,但是我们使用git-hooks来使这更容易,但事实证明,在 git 2.14 中,你可以设置 git config submodule.recurse 为true,以便在您拉动时更新子模块到你的git存储库。

如果它们在分支上,这会产生推动所有子模块发生变化的副作用,但是如果你已经需要这种行为,那么这可以完成这项工作。

可以使用:

完成
git config submodule.recurse true

Git for windows 2.6.3

git submodule update --rebase --remote

我是通过改编 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

我有一个示例我的环境设置回购

这是从所有git存储库中提取的命令行,无论它们是否是子模块:

ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'

如果您在顶级git存储库中运行它,可以将&quot; $ ROOT&quot; 替换为

您现在需要做的只是一个简单的 git checkout

只需确保通过此全局配置启用它: git config --global submodule.recurse true

从回购的顶层:

git submodule foreach git checkout develop
git submodule foreach git pull

这将切换所有分支以开发和提取最新的

我认为你必须写一个脚本才能做到这一点。说实话,我可能会安装python来执行它,以便您可以使用 os.walk cd 到每个目录并发出相应的命令。除了批处理之外,使用python或其他一些脚本语言将允许您轻松添加/删除子项目而无需修改脚本。

备注:不是太简单的方式,但可行,并且它有自己独特的专业人士。

如果只想克隆存储库的 HEAD 版本,而只想克隆其所有子模块的 HEAD (即结帐“trunk”),那么就可以使用 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top