提取所有 git 子模块的最新版本的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1030169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:38:19  来源:igfitidea点击:

Easy way to pull latest of all git submodules

gitgit-submodules

提问by Brad Robinson

We're using git submodules to manage a couple of large projects that have dependencies on many other libraries we've developed. Each library is a separate repo brought into the dependent project as a submodule. During development, we often want to just go grab the latest version of every dependent submodule.

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

Does git have a built in command to do this? If not, how about a Windows batch file or similar that can do it?

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

回答by Henrik Gustafsson

If it's the first timeyou check-out a repo you need to use --initfirst:

如果这是你第一次签出一个仓库,你需要先使用--init

git submodule update --init --recursive

For git 1.8.2or above, the option --remotewas added to support updating to latest tips of remote branches:

对于git的1.8.2或以上,则该选项--remote被添加到支持更新到远程分支的最新提示:

git submodule update --recursive --remote

This has the added benefit of respecting any "non default" branches specified in the .gitmodulesor .git/configfiles (if you happen to have any, default is origin/master, in which case some of the other answers here would work as well).

这具有尊重.gitmodules.git/config文件中指定的任何“非默认”分支的额外好处(如果您碰巧有任何,默认为 origin/master,在这种情况下,这里的其他一些答案也适用)。

For git 1.7.3or above you can use (but the below gotchas around what update does still apply):

对于git 1.7.3或更高版本,您可以使用(但以下关于更新的问题仍然适用):

git submodule update --recursive

or:

或者:

git pull --recurse-submodules

if you want to pull your submodules to latest commits instead of the current commit the repo points to.

如果你想把你的子模块拉到最新的提交而不是当前提交的 repo 指向。

See git-submodule(1)for details

混帐子模块(1)了解详细信息

回答by Alexander Bartosh

git pull --recurse-submodules --jobs=10

a feature git first learned in 1.8.5.

一个特性 git 在 1.8.5 中首次学习。

Until the bugis fixed, for the first time you do need to run

错误修复之前,您第一次确实需要运行

git submodule update --init --recursive

git 子模块更新 --init --recursive

回答by abc123

On init running the following command:

在 init 上运行以下命令:

git submodule update --init --recursive

from within the git repo directory, works best for me.

从 git repo 目录中,最适合我。

This will pull all latest including submodules.

这将拉取所有最新的包括子模块。

Explained

解释

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.


After this you can just run:

在此之后,您可以运行:

git submodule update --recursive

from within the git repo directory, works best for me.

从 git repo 目录中,最适合我。

This will pull all latest including submodules.

这将拉取所有最新的包括子模块。

回答by gahooa

Note: This is from 2009 and may have been good then but there are better options now.

注意:这是从 2009 年开始的,当时可能很好,但现在有更好的选择。

We use this. It's called git-pup:

我们用这个。它被称为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

Just put it in a suitable bin directory (/usr/local/bin). If on Windows, you may need to modify the syntax to get it to work :)

只需将其放在合适的 bin 目录 (/usr/local/bin) 中即可。如果在 Windows 上,您可能需要修改语法以使其正常工作:)

Update:

更新:

In response to the comment by the original author about pulling in all of the HEADs of all of the submodules -- that is a good question.

为了回应原作者关于拉入所有子模块的所有 HEAD 的评论——这是一个很好的问题。

I am pretty sure that gitdoes not have a command for this internally. In order to do so, you would need to identify what HEAD really is for a submodule. That could be as simple as saying masteris the most up to date branch, etc...

我很确定在git内部没有此命令。为此,您需要确定子模块的 HEAD 到底是什么。这可能就像说master是最新的分支一样简单,等等......

Following this, create a simple script that does the following:

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

  1. check git submodule statusfor "modified" repositories. The first character of the output lines indicates this. If a sub-repo is modified, you may NOT want to proceed.
  2. for each repo listed, cd into it's directory and run git checkout master && git pull. Check for errors.
  3. At the end, I suggest you print a display to the user to indicate the current status of the submodules -- perhaps prompt them to add all and commit?
  1. 检查git submodule status“修改过的”存储库。输出行的第一个字符表明了这一点。如果子回购被修改,您可能不想继续。
  2. 对于列出的每个 repo, cd 进入它的目录并运行git checkout master && git pull. 检查错误。
  3. 最后,我建议您向用户打印一个显示以指示子模块的当前状态——也许提示他们添加所有并提交?

I'd like to mention that this style is not really what git submodules were designed for. Typically, you want to say "LibraryX" is at version "2.32" and will stay that way until I tell it to "upgrade".

我想提一下,这种风格并不是 git 子模块的真正设计目标。通常,您想说“LibraryX”的版本为“2.32”,并且会一直保持这种状态,直到我告诉它“升级”。

That is, in a sense, what you are doing with the described script, but just more automatically. Care is required!

也就是说,从某种意义上说,您正在使用所描述的脚本做什么,但只是更自动。需要呵护!

Update 2:

更新 2:

If you are on a windows platform, you may want to look at using Python to implement the script as it is very capable in these areas. If you are on unix/linux, then I suggest just a bash script.

如果你在 windows 平台上,你可能想看看使用 Python 来实现脚本,因为它在这些领域非常有能力。如果您使用的是 unix/linux,那么我建议您只使用 bash 脚本。

Need any clarifications? Just post a comment.

需要任何说明吗?只需发表评论。

回答by mturquette

Henrik is on the right track. The 'foreach' command can execute any arbitrary shell script. Two options to pull the very latest might be,

亨里克走在正确的轨道上。'foreach' 命令可以执行任意的 shell 脚本。拉最新的两个选项可能是,

git submodule foreach git pull origin master

and,

和,

git submodule foreach /path/to/some/cool/script.sh

That will iterate through all initializedsubmodules and run the given commands.

这将遍历所有初始化的子模块并运行给定的命令。

回答by zachleat

The following worked for me on Windows.

以下在 Windows 上对我有用。

git submodule init
git submodule update

回答by Sebastien Varrette

As it may happens that the default branch of your submodules is notmaster, this is how I automate the full Git submodules upgrades:

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

回答by antitoxic

Edit:

编辑

In the comments was pointed out (by philfreo) that the latest version is required. If there is any nested submodules that need to be in their latest version :

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

git submodule foreach --recursive git pull

-----Outdated comment below-----

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

Isn't this the official way to do it ?

这不是官方的做法吗?

git submodule update --init

I use it every time. No problems so far.

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

Edit:

编辑:

I just found that you can use:

我刚刚发现你可以使用:

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

Which will also recursively pull all of the submodules, i.e. dependancies.

这也将递归地拉出所有子模块,即依赖项。

回答by Yash

First time

第一次

Clone and Init Submodule

克隆和初始化子模块

git clone [email protected]:speedovation/kiwi-resources.git resources
git submodule init

Rest

休息

During development just pull and update submodule

在开发过程中只需拉取和更新子模块

git pull --recurse-submodules  && git submodule update --recursive

Update Git submodule to latest commit on origin

将 Git 子模块更新为最新的原始提交

git submodule foreach git pull origin master

Preferred way should be below

首选方式应该在下面

git submodule update --remote --merge

note: last two commands have same behaviour

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

回答by Jens Kohl

I don't know since which version of git this is working, but that's what you're searching for:

我不知道这是从哪个版本的 git 开始工作的,但这就是你正在寻找的:

git submodule update --recursive

I use it with git pullto update the root repository, too:

我也用它git pull来更新根存储库:

git pull && git submodule update --recursive