如何将已克隆的项目添加为子模块? [英] How to add already cloned projects as submodules?
问题描述
我在文件夹中有很多我克隆了很长时间的项目;最近我将这个整个文件夹移到我的回购站中,并希望将这些克隆的项目转换为子模块,以便将来能够更好地进行更新和控制。我已经搜索了很多关于如何做到这一点,但所有的教程只讲述如何添加子模块新鲜。任何人都可以帮助我?
将现有git存储库添加为子模块与添加新模块相同。
- 首先将包含所有git存储库的文件夹作为一个git存储库,并使用
git init
。 - 使用
git submodule add https // example.com / remoterepo ./localrepo
,其中./ localrepo
是你现有的git仓库。
- 注意:您从
localrepo / .git获得
。remoterepo
的URL / config
- 注意:您从
- 对您想添加为子模块的所有现有存储库重复第二步。
- 现在您可以运行
git submodule foreach git pull
来更新您的所有子项目。
如果您有很多子模块,您可能需要编写一个小脚本来自动执行第二步,这应该不困难。
编辑
以下是我用于重现评论中提到的错误的内容。我检查了三次命令,并且仍然看不到错误:
git - 版本
mkdir子模块测试
cd子模块测试
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
echotest 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测试提交回购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'我们刚刚为remote_repo_B'
添加了一个子模块git子模块状态
使用以下命令检查 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或错误。但是编辑被拒绝了,即使是现在,因为它完全是错误的,我已经解释了为什么在评论中,我对任何人都没有冒犯性,但是这浪费了我的时间,如果答案对于你再一次,如果你没有改善它,不要编辑我的答案。
I have in a folder a large number of projects that I cloned for quite some time; recently I moved this whole folder into one of my repos and would like to convert these cloned projects into submodules so that I can better update and control in the future. I've googled a lot on how to do this, but all the tutorials only talk about how to add submodules fresh. Anyone can help me?
Adding existing git repository as a submodule is the same as adding a new one.
- First make the folder that contains all your git repositories as itself a git repository with
git init
. - Use
git submodule add https//example.com/remoterepo ./localrepo
, where./localrepo
is your existing git repository.- Note: You get the URL for
remoterepo
fromlocalrepo/.git/config
.
- Note: You get the URL for
- Repeat the second step for all your existing repositories that you want to add as submodules.
- Now you can run
git submodule foreach git pull
to update all your subprojects.
You may want to write a small script to automate the second step if you have lots of submodules, which shouldn't be difficult.
Edit
The following is what I used for trying to reproduce the error mentioned in the comments. I triple checked the commands, and I still can't see the error:
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
Use the following command to check current status of local_repo_A
, it has only two blob objects, one for 'test.txt' and another for the implicitly created '.gitmodules' file, nothing from remote_repo_B
are added to the index of local_repo_A
.
find .git/objects -type f | awk -F/ '{print $3$4}' | xargs -I {} git cat-file -t {} | grep blob
The reason I rolled back the edits is because the edits is simply WRONG, it was already rejected by two other mediators and me, but later some other guy approved it, I am not sure if it is a bug of SO or what. But the edits are rejected, even now because it's totally wrong, I already explained why in the comments, I am no offensive to anyone, but this wasted my time, downvote it if the answer is not helpful to you.
Again, if you are not improving it, DON'T edit my answer.
这篇关于如何将已克隆的项目添加为子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!