自动添加git子模块(.gitmodules) [英] Adding git submodules automatically (.gitmodules)

查看:102
本文介绍了自动添加git子模块(.gitmodules)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个子模块的仓库.我想添加一些其他内容,而对我而言,最快的方法是使用 .gitmodules (我认为这显然应该允许任何类型的子模块管理).

I have a repo with several submodules. I want to add some others, and the fastest way for me is to use the .gitmodules (which in my opinion should clearly allow any kind of submodule management).

但是,在编辑此文件并添加子模块时,在 git子模块init 之后没有添加任何内容(修改前已经存在的子模块除外).

However, when editing this file and adding submodules, after a git submodule init nothing is added (except the submodules that were already present before the modification).

有没有什么解决方案可以添加子模块,而无需通过 git子模块添加(即,仅通过编辑 .gitmodules 文件然后进行 git子模块更新--init )吗?

Is there any solution to add a submodule without going through git submodule add (ie, just by editing the .gitmodules file and then git submodule update --init) ?

也就是说,以下工作流程应自动添加子模块" foo/bar ":

That is, the following workflow should automatically add the submodule "foo/bar":

Add the following to .gitmodules:
    [submodule "foo/bar"]
        path = foo/bar
        url = https://example.com/foo.git

Run the following command after saving:
    git submodule init
    git submodule update

Expected result:
    submodule 'foo/bar' automatically gets added
    it is also updated (the update command)

推荐答案

添加Git子模块时,Git会创建 .gitmodules 文件,并为名为 git-submodule 将添加如下内容:

When you add a Git submodule, Git creates .gitmodules file and for a submodule named git-submodule will add something like this:

[submodule "git-submodule"]
    path = git-submodule
    url = https://github.com/TomasHubelbauer/git-submodule

将相同内容添加到该文件中现有内容之后的 .git/config 中.

The same is added to .git/config after the existing content in that file.

.git/modules 中创建以子模块命名的子模块的文件夹.该文件夹几乎与实际子模块存储库的 .git 目录相同,但是它不包含实际的 objects (而是将子模块数据检出到其目录及其元数据在这里.)

A folder for the submodule named after the submodule is created in .git/modules. This folder is nearly identical to the the .git directory of the actual submodule repository, but it doesn't contain the actual objects (instead the submodule data is checked out to its directory and its metadata are here).

这意味着从理论上讲,您可以手动添加子模块,而无需使用 git submodule add ,但是您必须重新创建所有这些配置文件.但是仍然可以想象将子模块存储库克隆到一个单独的目录,并将其 .git 复制到该目录中.可能有效.

This means that theoretically, you might be able to add a submodule by hand without using git submodule add, but you would have to recreate all these config files. But one can still imagine cloning the submodule repository to a separate directory and copying its .git over to this one. That might work.

但是,添加子模块也会更改索引 .git/index ,因此您还必须手动更新此哈希,此时,您将重新实现Git,但手动进行

However, adding a submodule also changes the index, .git/index, so you would have to manually update this hash as well, and at this point, you're reimplementing Git, but manually.

因此,我认为手动添加Git子模块几乎不可行.

As a result, I don't believe it is anywhere near practical to add a Git submodule by hand.

这篇关于自动添加git子模块(.gitmodules)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆