我在一个文件夹中有很多项目,我克隆了很长一段时间;最近我将整个文件夹移动到我的一个repos中,并希望将这些克隆项目转换为子模块,以便将来更好地更新和控制。我已经搜索了很多关于如何执行此操作的内容,但所有教程仅讨论了如何添加子模块。有人可以帮帮我吗?
将现有git存储库添加为子模块与添加新模块相同。
git init
的git存储库。git submodule add https//example.com/remoterepo ./localrepo
,其中./localrepo
是您现有的git存储库。
注意:您从remoterepo
获取localrepo/.git/config
的URL。git submodule foreach git pull
来更新所有子项目。如果你有很多子模块,你可能想写一个小脚本来自动化第二步,这应该不难。
以下是我用于尝试重现注释中提到的错误的内容。我三重检查命令,我仍然看不到错误:
git --version
mkdir submoduletest
cd submoduletest
git init --bare remote_repo_A
git init --bare remote_repo_B
git clone remote_repo_A local_repo_A
git clone remote_repo_B local_repo_B
cd local_repo_A
echo "test commit on repo B" >> test.txt
git add test.txt
git commit -m 'test commit message'
git push origin master
cd ../local_repo_B
echo "test commit on repo B" >> test.txt
git add test.txt
git commit -m 'test commit message'
git push origin master
cd ../local_repo_A
git clone ../remote_repo_B local_repo_B
git submodule add ../remote_repo_B ./local_repo_B
git submodule foreach git pull origin master
git add .
git ci -m 'we just added a submodule for remote_repo_B'
git submodule status
使用以下命令检查local_repo_A
的当前状态,它只有两个blob对象,一个用于'test.txt',另一个用于隐式创建的'.gitmodules'文件,remote_repo_B
中没有任何内容被添加到local_repo_A
的索引中。
find .git/objects -type f | awk -F/ '{print $3$4}' | xargs -I {} git cat-file -t {} | grep blob
我回滚编辑的原因是因为编辑只是错误,它已被其他两个调解员和我拒绝,但后来其他一些人批准了它,我不确定它是否是SO的错误或者是什么。但编辑被拒绝了,即使是现在因为它完全错了,我已经解释了为什么在评论中,我不会冒犯任何人,但是这浪费了我的时间,如果答案对你没有帮助,请将其投票。
同样,如果你没有改进它,请不要编辑我的答案。
我将添加@ neevek的答案,如果你得到already exists in the index
错误,那么你可以尝试这个版本:
git rm --cached ./localrepo
git submodule add https//example.com/remoterepo ./localrepo
,其中./localrepo
是您现有的git存储库。
注意:您从remoterepo
获取localrepo/.git/config
的URL。git submodule foreach git pull
来更新所有子项目。如果您有许多子模块,您可能需要编写一个小脚本来自动执行第二步。
为了添加许多子模块,我写了这个简单的循环:
for repo in vim/bundle/*
do
echo $repo
pushd $repo
url=$(git remote get-url $(git remote))
echo $url
popd
git submodule add $url ./$repo
done
明显的限制我没有打扰修复:
git remote
实际上返回所有遥控器,而不仅仅是当前,因此如果有多个,脚本将会中断