拉取所有 git 子模块的最新更改
我们使用 git 子模块来管理几个大型项目,这些项目依赖于我们开发的许多其他库。 每个库都是一个单独的存储库,作为子模块引入依赖项目中。 在开发过程中,我们通常只想获取每个依赖子模块的最新版本。
如何提取所有 git 子模块的最新更改?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
如果这是第一次您签出存储库,您需要首先使用
--init
:对于git 1.8.2或更高版本,添加了选项
--remote
以支持更新到远程分支的最新提示:这具有尊重
.gitmodules
或中指定的任何“非默认”分支的额外好处code>.git/config
文件(如果你碰巧有的话,默认是 origin/master,在这种情况下,这里的其他一些答案也可以工作)。对于 git 1.7.3 或更高版本,您可以使用(但以下有关更新的问题仍然适用):
或者:
如果您想将子模块拉到最新提交而不是当前提交,则存储库点到。
有关详细信息,请参阅 git-submodule(1)
If it's the first time you check-out a repo you need to use
--init
first:For git 1.8.2 or above, the option
--remote
was added to support updating to latest tips of remote branches:This has the added benefit of respecting any "non default" branches specified in the
.gitmodules
or.git/config
files (if you happen to have any, default is origin/master, in which case some of the other answers here would work as well).For git 1.7.3 or above you can use (but the below gotchas around what update does still apply):
or:
if you want to pull your submodules to latest commits instead of the current commit the repo points to.
See git-submodule(1) for details
直到 bug< /a> 是固定的,第一次你确实需要运行
Until the bug is fixed, for the first time you do need to run
在 init 上运行以下命令:
从 git repo 目录中运行,最适合我。
这将拉出所有最新的,包括子模块。
解释
之后你可以运行:
从 git repo 目录中运行,最适合我。
这将拉出所有最新的,包括子模块。
On init running the following command:
from within the git repo directory, works best for me.
This will pull all latest including submodules.
Explained
After this you can just run:
from within the git repo directory, works best for me.
This will pull all latest including submodules.
注意:这是 2009 年的内容,当时可能不错,但现在有更好的选择。
我们使用它。 它称为 git-pup:
只需将其放在合适的 bin 目录(/usr/local/bin)中即可。 如果在 Windows 上,您可能需要修改语法才能使其工作:)
更新:
响应原作者关于拉入所有子模块的所有 HEAD 的评论 --这是一个好问题。
我非常确定 git 内部没有为此的命令。 为此,您需要确定子模块的 HEAD 到底是什么。 这可能很简单,比如说
master
是最新的分支,等等...在此之后,创建一个简单的脚本来执行以下操作:
git 子模块状态
对于“修改的”存储库。 输出行的第一个字符表明了这一点。 如果子存储库被修改,您可能不想继续。我想提一下,这种风格并不是 git 子模块的真正设计目的。 通常,您想说“LibraryX”的版本为“2.32”,并且将保持这种状态,直到我告诉它“升级”。
从某种意义上说,这就是您正在使用所描述的脚本所做的事情,但只是更加自动。 需要护理!
更新2:
如果您使用的是Windows平台,您可能需要考虑使用Python来实现脚本,因为它在这些领域非常强大。 如果您使用的是 unix/linux,那么我建议只使用 bash 脚本。
需要任何澄清吗? 只需发表评论即可。
Note: This is from 2009 and may have been good then but there are better options now.
We use this. It's called
git-pup
: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 :)
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.
I am pretty sure that
git
does 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 sayingmaster
is the most up to date branch, etc...Following this, create a simple script that does the following:
git submodule status
for "modified" repositories. The first character of the output lines indicates this. If a sub-repo is modified, you may NOT want to proceed.git checkout master && git pull
. Check for errors.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".
That is, in a sense, what you are doing with the described script, but just more automatically. Care is required!
Update 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.
Need any clarifications? Just post a comment.
Henrik 走在正确的道路上。 git submodule foreach 命令可以执行任意 shell 脚本。 获取最新版本的两个选项可能是:
和:
这将迭代所有初始化子模块并运行给定的命令。
Henrik is on the right track. The
git submodule foreach
command can execute any arbitrary shell script. Two options to pull the very latest might be:and:
That will iterate through all initialized submodules and run the given commands.
以下内容在 Windows 上对我有用。
The following worked for me on Windows.
对我来说,git 2.24.03,更新到 .gitmodules 中定义的远程分支的最新提交。
git submodule update --recursive --init
git submodule update --recursive --remote
git version 2.24.3 (Apple Git-128)
请注意:< /强>
有人这么说
git pull --recurse-submodules 与 git submodule update --recursive --remote 相同。 但根据我的测试, git pull --recurse-submodules 可能无法更新到 .gitmodules 中定义的远程分支的最新提交。
For me, git 2.24.03, get updated to latest commit of remote branches defined in .gitmodules.
git submodule update --recursive --init
git submodule update --recursive --remote
git version 2.24.3 (Apple Git-128)
Please Note:
Someone said that
git pull --recurse-submodules
is the same asgit submodule update --recursive --remote
. But from my test,git pull --recurse-submodules
may not get updated to latest commit of remote branches defined in .gitmodules.第一次
克隆和初始化子模块
休息
在开发期间只需拉取和更新子模块
将 Git 子模块更新到源上的最新提交
首选方式应低于
注意:最后两个命令具有相同的行为
First time
Clone and Init Submodule
Rest
During development just pull and update submodule
Update Git submodule to latest commit on origin
Preferred way should be below
note: last two commands have same behaviour
编辑:
在评论中(philfreo)指出需要最新版本。 如果有任何嵌套子模块需要使用最新版本:
-----下面过时的评论-----
这不是官方的方法吗?
我每次都用它。 到目前为止没有问题。
编辑:
我刚刚发现您可以使用:
这也会递归地提取所有子模块,即依赖项。
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 :
-----Outdated comment below-----
Isn't this the official way to do it ?
I use it every time. No problems so far.
Edit:
I just found that you can use:
Which will also recursively pull all of the submodules, i.e. dependancies.
由于子模块的默认分支可能不是
master
,这就是我自动执行完整 Git 子模块升级的方法:As it may happens that the default branch of your submodules is not
master
, this is how I automate the full Git submodules upgrades:我不知道从哪个版本的 git 开始它就可以工作,但这就是您正在寻找的:
我也将它与 git pull 一起使用来更新根存储库:
I don't know since which version of git this is working, but that's what you're searching for:
I use it with
git pull
to update the root repository, too:上面的答案很好,但是我们使用 git-hooks 来使这更容易,但事实证明在 git 2.14 中,您可以设置
git config submodule.recurse
设置为 true,以便在拉取到 git 存储库时更新子模块。然而,如果它们位于分支上,这将产生推送所有子模块更改的副作用,但如果您已经需要该行为,则这可以完成这项工作。
可以通过使用以下方法来完成:
The above answers are good, however we were using git-hooks to make this easier but it turns out that in git 2.14, you can set
git config submodule.recurse
to true to enable submodules to to updated when you pull to your git repository.This will have the side effect of pushing all submodules change you have if they are on branches however, but if you have need of that behaviour already this could do the job.
Can be done by using:
根据从远程提取每个子模块的“最新”代码的现有答案来澄清一些事情。
如果“latest”表示已签入的子模块指针,则务必使用:
如果“latest”表示最新的main,那么这样的事情就可以工作:
不幸的是,这意味着没有“--jobs”选项,所以我们不能并行运行它。 我见过的最接近并行运行的方法是使用 pfs python 代码。
To clarify a few things based on already available answers of pulling "latest" code of each submodule from remote.
If "latest" means the submodule pointers that were checked in, then by all means use:
If "latest" means the latest of main, then something like this can work:
Unfortunately, this means there's no "--jobs" option, so we cannot run it in parallel. The closest I've seen to running this in parallel is by using the pfs python code.
我经常使用这个命令,到目前为止它有效。
希望这更快。
I often use this commands, it works so far.
Hope this faster.
正如抗毒的答案<中所述/a>,一个简单的 git submodule foreach --recursive git pull 就足够了。
弗朗西斯·培根的答案< /a> 注释
git pull --recurse-submodules
可能有所不同。您可以使用详细选项进行测试并查看发生了什么:
但为此,您将需要 Git 2.40(2023 年第一季度)。
“
git pull -v --recurse-submodules
“(man) 尝试将-v
传递给底层git 子模块更新
(man),哪个不明白请求并被拒绝:Git 2.40(2023 年第一季度)已更正此问题。请参阅 提交 6f65f84(2022 年 12 月 10 日),作者:Sven Strickroth (
csware
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 b3b9e5c,2022 年 12 月 28 日)不再有“呕吐”(即显示用法 '
git submodule foreach [--quiet] [--recursive] [--]
'),因为-v< /code> 是 git 子模块的未知选项。
As noted in antitoxic's answer, a simple
git submodule foreach --recursive git pull
can be enough.Francis Bacon's answer notes
git pull --recurse-submodules
can differ.You could test out and see what is going on with a verbose option:
But for that, you will need Git 2.40 (Q1 2023).
"
git pull -v --recurse-submodules
"(man) attempted to pass-v
down to underlyinggit submodule update
(man), which did not understand the request and barfed: this has been corrected with Git 2.40 (Q1 2023).See commit 6f65f84 (10 Dec 2022) by Sven Strickroth (
csware
).(Merged by Junio C Hamano --
gitster
-- in commit b3b9e5c, 28 Dec 2022)No more "barfing" (IE. displaying the usage '
git submodule foreach [--quiet] [--recursive] [--] <command>
') because the-v
was an unknown option forgit submodule
.适用于 Windows 的 Git 2.6.3:
git submodule update --rebase --remote
Git for windows 2.6.3:
git submodule update --rebase --remote
从存储库的顶层:
这会将所有分支切换为开发并拉取最新版本
From the top level in the repo:
This will switch all branches to develop and pull latest
我通过改编 gahooa 的 上面的答案:
将其与git集成
[alias]
...如果您的父项目在
.gitmodules中有类似的内容:
在你的 .gitconfig 中添加类似的内容
然后更新你的子模块,运行:
我有一个 示例位于我的环境设置存储库 。
I did this by adapting gahooa's answer above:
Integrate it with a git
[alias]
...If your parent project has something like this in
.gitmodules
:Add something like this inside your .gitconfig
Then to update your submodules, run:
I have an example of it in my environment setup repo.
您现在需要做的就是一个简单的 git checkout
只需确保通过此全局配置启用它: git config --global submodule.recurse true
All you need to do now is a simple
git checkout
Just make sure to enable it via this global config:
git config --global submodule.recurse true
以下是从所有 git 存储库中提取的命令行,无论它们是否是子模块:
如果您在顶级 git 存储库中运行它,则可以将
"$ROOT"
替换为.
。Here is the command-line to pull from all of your git repositories whether they're or not submodules:
If you running it in your top git repository, you can replace
"$ROOT"
into.
.我编写了这个简单的 shell 脚本,它对我来说效果很好。
I worked on this simple shell script which works fine for me.
备注:不是太简单的方法,但可行,并且有其独特的优点。
如果只想克隆存储库的
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。Remark: not too easy way, but workable and it has its own unique pros.
If one want to clone only
HEAD
revision of a repository and onlyHEAD
s of all the its submodules (i.e. to checkout "trunk"), then one can use following Lua script. Sometimes simple commandgit submodule update --init --recursive --remote --no-fetch --depth=1
can result in an unrecoverablegit
error. In this case one need to clean up subdirectory of.git/modules
directory and clone submodule manually usinggit clone --separate-git-dir
command. The only complexity is to find out URL, path of.git
directory of submodule and path of submodule in superproject tree.Remark: the script is only tested against
https://github.com/boostorg/boost.git
repository. Its peculiarities: all the submodules hosted on the same host and.gitmodules
contains only relative URLs.我认为你必须编写一个脚本来做到这一点。 老实说,我可能会安装 python 来执行此操作,以便您可以使用 os.walk cd 到每个目录并发出适当的命令。 使用 python 或其他脚本语言(而不是批处理)可以让您轻松添加/删除子项目,而无需修改脚本。
I think you'll have to write a script to do this. To be honest, I might install python to do it so that you can use
os.walk
tocd
to each directory and issue the appropriate commands. Using python or some other scripting language, other than batch, would allow you to easily add/remove subprojects with out having to modify the script.