简体   繁体   English

将克隆的git存储库转换为子模块

[英]Converting cloned git repositories into submodules

I have a project which is a git repository, and inside that project I have some cloned git repositories. 我有一个项目,这是一个git存储库,并且在该项目中,我有一些克隆的git存储库。

The directory structure is like this. 目录结构是这样的。

main-project
    -other_project (cloned git repository)
    -another_project (cloned git repository)

When I tried to clone main-project into another system, it only has empty directories for other_project and another_project. 当我尝试将main-project克隆到另一个系统中时,other_project和another_project只有空目录。

I know that I should have used git submodule to add those repositories in the first place. 我知道我应该首先使用git submodule添加这些存储库。 Since I didn't, is there any way to fix the mess I've made ? 既然我没有,有什么办法可以解决我的烂摊子?

There are about 15 repositories inside (which should be submodules), so a way to "bulk convert" all those repositories into submodules would be greatly appreciated. 内部大约有15个存储库(应该是子模块),因此,将所有存储库“批量转换”为子模块的方法将不胜感激。

You would need to create a script, a bit like in " How to do a bash foreach csv field to 'git submodule add' ", except, for each nested repo, you would need to extract the upstream repo url ( git remote origin ) 您将需要创建一个脚本,有点像“ 如何对bash foreach csv字段进行'git submodule add'的操作 ”,除了,对于每个嵌套的回购,您都需要提取上游回购url( git remote origin )。

Then add the .gitmodules file created in your parent repo by those " submodule add " , and commit. 然后添加由那些submodule add在父.gitmodules创建的.gitmodules文件,并提交。

You will be able to clone (with --recursive option) your parent repo, and get back all those submodules. 您将能够克隆 (使用--recursive选项)您的父仓库,并取回所有这些子模块。

You can also just do git submodule add ./path/to/subdirectory ./path/to/subdirectory , which will add the repositories as submodules, but with the incorrect origin url. 您也可以只执行git submodule add ./path/to/subdirectory ./path/to/subdirectory ,这会将存储库添加为子模块,但原始URL不正确。 You can then manually edit .git/config and replace the urls with the correct urls (ie the ones you cloned from). 然后,您可以手动编辑.git/config并将其替换为正确的URL(即从中克隆的URL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM