将子模块添加到现有目录中的 Git

5

我有一个git仓库在父文件夹中,我们称这个文件夹为 "root"。然后在文件夹中有一个子目录,我们称之为 "child"。我想做的是将远程仓库作为子模块克隆到 "child" 中:

git submodule add git@github.com:username/repopath child/
git submodule add git@github.com:username/repopath ./child/

以上两者都给了我以下错误:

child already exists in the index(子项已存在于索引中)

我尝试将目录从跟踪列表中删除:

git rm --cached .\child\*
git submodule add git@github.com:username/repopath child/

然后我遇到了这个错误:

'child'已经存在且不是有效的git仓库

任何帮助都将不胜感激

谢谢

1个回答

6

首先,在新的克隆中尝试您的子模块添加命令,因为之前的尝试可能会导致子模块处于部分状态。

其次,首先尝试

git rm -r --cached child

然后,添加并提交。

最后,尝试使用git submodule命令

git submodule add -- git@github.com:username/repopath child

添加并提交。


2
对于学习者来说,git submodule add -- git@github.com:username/repopath child--:两个短横线后面只有文件夹和文件,没有选项。 git@githu... 是一个 ssh 地址,而不是 https - Timo
2
@Timo 关于双破折号(或双横线)的更多信息,请参见:https://dev59.com/iXM_5IYBdhLWcg3w3nRs#1192194 - VonC
1
非常感谢@VonC的回答和评论。非常有用。 - Son Nguyen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接