如何在现有仓库中创建子模块

How to create submodule in existing repo

我是 git 的新手,新项目的文件夹结构如下,所以我去 visual studio 创建了一个空项目,它的文件夹结构如下:

project: (Repo) 
folder1 (sub1)
folder2 (sub2)

我在本地使用 git。
我使用 sourcetree 为项目创建了一个 repo,
现在我无法添加任何子模块,我不知道为什么。

每当我尝试在 powershell 中:

PS E:\Projects\Project> git submodule add ./sub1

我收到以下错误:

sub1 already exists in the index

然后我决定使用 git rm -r sub1 删除 sub1,然后再次添加它,然后当我再次尝试使用 git submodule add 添加它时,我收到了这个新错误:

sub1 already exists and is not a valid git repo

那我做错了什么?

Adding a submodule 意味着在现有的一个 git 中克隆另一个 repo 并保留对它的引用。

在您的情况下,您没有指定要添加的存储库:

git submodule add -- /url/of/sub1/repo sub1

如果 sub1sub2 不应该是它们自己的 git 回购,而是主回购文件夹的简单子文件夹,那么你不需要 git submodule add命令。
只需在 sub1sub2 中添加文件,您就可以 git addgit commit 它们。
不涉及子模块。

您只需要在您的根文件夹中,然后添加子模块文件夹。

git submodule add <url>

现在,当您克隆项目时,您只需初始化并更新子模块

git submodule init
git submodule update

Git 1.8.2 提供了一个新选项 --remote

git submodule update --remote --merge

会从每个子模块中的上游获取最新的更改,将它们合并,并检查子模块的最新修订。正如 [文档][1] 所说:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

这相当于运行 git拉入每个子模块。