如何拉取一个新的子模块

How to pull a new submodule

尝试在本网站和其他网站上寻找答案: Whosebug - Easy way pull latest of all submodules

他们似乎都想谈论你是否在控制他们,而不是如果其他人添加了一个,我只想将额外的一个拉到我的项目中,而不必在需要时存储或备份我的更改删除文件夹。

我是否应该删除 .gitmodules 文件,and/or 我已经用 git clone --recursive 下拉的子模块目录? (Whosebug - How to git clone including submodules?)

这些命令似乎也没有帮助:

我每次都手动仔细检查库目录,以确保是否出现了额外的子模块。

我想避免执行某些操作,除非它们不会破坏我当前包含代码更改的存储库状态,并解决我的问题,以防它是我提到但没有提到的命令 运行,或者其他人有另一个尝试。

我可以更努力地尝试其中的一些,但我想我暂时不想再惹他们了,而且由于我在网上搜索后还没有找到这个问题的答案,也许是有希望的和最终的答案无论如何都会帮助其他人。

我是否患有这里提到的骗局? Software Engineering - Git submodule vs Git clone

更多链接:

到目前为止我收到的最好建议是 运行 这个命令:

git submodule add <URL_to_submodule> <local_path_to_place_submodule>

所以它看起来是其他贡献者会做的,我会再做一次,即使它已经存在于远程。

我想这在技术上并没有像预期的那样从远程数据更新 .gitmodules 文件,但还没有找到方法来做到这一点。

帮助归功于 @pandatrax

更新

在尝试 add 方法之前,我尝试了另外一个想法,涉及手动从远程复制 .gitmodules 文件并尝试任何更新命令,但遗憾的是该方法也不起作用.如果我在根目录中执行命令,它可能会有所不同,因为我在一个子文件夹中,但我对此表示怀疑。

然后我使用了add的方法,下载了依赖,但是.gitmodules文件显示有变化。一旦我设置了 GitHub 远程并在丢弃该文件后从中拉出,项目现在似乎处于更好的状态,甚至同步更新模块或 2 的提交 SHA,因为它们匹配,或者它被覆盖了。

git submodule update --init local/path/to/submodule/folder

你必须做两件事:

  1. 在包含子模块的主存储库中执行 git pull。这会将新的子模块添加为一个空目录。
  2. 在主存储库中执行 git submodule update --recursive --remote。这将拉取所有子模块的最新更改,包括新的子模块。

这至少在 Git 2.13 中有效。另请注意,如果存储库和子模块位于 GitHub 上,您必须确保您拥有对它们的访问权限(如果它们是私有的)。

这应该可以工作,只要新的子模块在远程的 .gitmodules 中

git pull --recurse-submodules

当我通过以下命令更新子模块时,我的子模块已解决:

git pull <URL_to_submodule_or_repository> <Name_of_branche>

not if someone else added one, and I just want to pull the additional one into my project without having to stash or backup my changes if I need to delete the folder.

这应该与 Git 2.36(2022 年第 2 季度)一起更好地工作:当“git fetch --recurse-submodules"(man) 抓取子模块提交时,需要递归地检查新获取的提交超级项目,它只关注超级项目当前签出的子模块。

Git now does so for all submodules that have been run "git submodule init"(man) on.

参见 commit 5fff35d, commit b90d9f7, commit 5370b91, commit 73bc90d, commit 6e1e0c9, commit 1e5dd3a, commit 7c2f8cc, commit d1d1572, commit 6e94bd6, commit f3117df (07 Mar 2022) by Glen Choo (chooglen)
(由 Junio C Hamano -- gitster -- in commit dd9ff30 合并,2022 年 3 月 25 日)

fetch: fetch unpopulated, changed submodules

Signed-off-by: Glen Choo

"git fetch --recurse-submodules"(man) only considers populated submodules (i.e.
submodules that can be found by iterating the index), which makes "git fetch"(man) behave differently based on which commit is checked out.
As a result, even if the user has initialized all submodules correctly, they may not fetch the necessary submodule commits, and commands like "git checkout --recurse-submodules"(man) might fail.

Teach "git fetch to"(man) fetch cloned, changed submodules regardless of whether they are populated.
This is in addition to the current behavior of fetching populated submodules (which is always attempted regardless of what was fetched in the superproject, or even if nothing was fetched in the superproject).

A submodule may be encountered multiple times (via the list of populated submodules or via the list of changed submodules).
When this happens, "git fetch" only reads the 'populated copy' and ignores the 'changed copy'.
Amend the verify_fetch_result() test helper so that we can assert on which 'copy' is being read.

fetch-options 现在包含在其 man page 中:

submodules should be fetched too. When recursing through submodules, git fetch always attempts to fetch "changed" submodules, that is, a submodule that has commits that are referenced by a newly fetched superproject commit but are missing in the local submodule clone. A changed submodule can be fetched as long as it is present locally e.g. in $GIT_DIR/modules/ (see linkgit:gitsubmodules[7]); if the upstream adds a new submodule, that submodule cannot be fetched until it is cloned e.g. by git submodule update.

When set to 'on-demand', only changed submodules are fetched. When set to 'yes', all populated submodules are fetched and submodules that are both unpopulated and changed are fetched. When set to 'no', submodules are never fetched.

When unspecified, this uses the value of fetch.recurseSubmodules if it is set (see git config), defaulting to 'on-demand' if unset. When this option is used without any value, it defaults to 'yes'.

git fetch 现在包含在其 man page 中:

Using --recurse-submodules can only fetch new commits in submodules that are present locally e.g. in $GIT_DIR/modules/. If the upstream adds a new submodule, that submodule cannot be fetched until it is cloned e.g. by git submodule update. This is expected to be fixed in a future Git version.