我有一个git repo,想要添加一个子模块。问题是,这个子模块存在于另一个repo的文件夹中。我可以只将该文件夹作为子模块添加吗?
我有一个git repo,想要添加一个子模块。问题是,这个子模块存在于另一个repo的文件夹中。我可以只将该文件夹作为子模块添加吗?
如果你确实需要将其他仓库的一部分包含在自己仓库的历史中,那么使用子树合并策略比子模块更适合。
- 将其他项目命名为“repo1”并获取它。
- 准备记录结果为合并的后续步骤。
- 读取“repo1”的“main”分支到子目录“repo1”中。
- 记录合并结果。
- 使用“subtree”进行后续合并以维护结果。
$ git remote add -f repo1 /path/to/repo1
$ git merge -s ours --no-commit --allow-unrelated-histories repo1/main
$ git read-tree --prefix=repo1/ -u repo1/main
$ git commit -m "Merge repo1 as our subdirectory"
$ git pull -s subtree repo1 main
但无论哪种情况,完整的存储库都链接到您的存储库中,而不仅仅是一个目录。
并且无法进行部分克隆。
您可以尝试将该目录分离为自己的存储库,然后将其作为子模块添加,但这意味着它的历史记录将与最初来自的存储库完全分离。
cd /path/to/repo1
git filter-repo --path repo1SubFolder
#Move the files inside repo1SubFolderto the root
git filter-repo --subdirectory-filter repo1SubFolder/
# Go to your new repository, add a remote to the original repository
cd /path/to/repo2
git remote add repo1 /path/to/repo1
# Pull files and history from this branch into repo2
# (containing only the directory you want to move) .
git pull repo1 main
git remote rm repo1