Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add already cloned projects as submodules?

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?

like image 324
L__ Avatar asked May 23 '13 22:05

L__


People also ask

How do I add an existing git repository to a submodule?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

Does git clone get submodules?

Cloning a Project with Submodules If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.

How do I initialize all submodules?

To initialize them you need run git submodule update --recursive --init . Quote from manual: If the submodule is not yet initialized, and you just want to use the setting as stored in . gitmodules, you can automatically initialize the submodule with the --init option.


Video Answer


1 Answers

Adding existing git repository as a submodule is the same as adding a new one.

  1. First make the folder that contains all your git repositories as itself a git repository with git init.
  2. Use git submodule add https//example.com/remoterepo ./localrepo, where ./localrepo is your existing git repository.
    • Note: You get the URL for remoterepo from localrepo/.git/config.
  3. Repeat the second step for all your existing repositories that you want to add as submodules.
  4. 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.

like image 169
neevek Avatar answered Sep 21 '22 13:09

neevek