My answer has two parts, Conditions, and Solution.
Part I: Conditions
I had the same problem, but I had already a similar repository with the same structure,
say
Project1 (not a repo)
|___ Repo1
|___ Repo2
and:
Project2 (a repo)
|___ Submodule1 (same repo as Repo1)
|___ Submodule2 (same repo as Repo2)
And I wanted to convert Repo1 and Repo2 to a submodule of Project1 that it is basically the same as Project2.
To state clear, cloning Project2 would not do the trick because there were a lot more files in Project1 and Project2 that were not the same, but to keep things simple..
So my Project1's .git/config
looked like this
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
And my Project2's .git/config
like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[submodule]
active = .
[remote "origin"]
url = ######
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "Submodule2"]
url = ######
[submodule "Subodule2"]
url = ######
and in Project2, there was a file called .gitmodules:
I had the same problem, but I had already a similar repository with the same structure,
say
Project1 (not a repo)
|___ Repo1
|___ Repo2
and:
Project2 (a repo)
|___ Submodule1 (same repo as Repo1)
|___ Submodule2 (same repo as Repo2)
And I wanted to convert Repo1 and Repo2 to a submodule of Project1 that it is basically the same as Project2.
To state clear, cloning Project2 would not do the trick because there were a lot more files in Project1 and Project2 that were not the same, but to keep things simple..
So my Project1's .git/config
looked like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
And my Project2's .git/config
like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[submodule]
active = .
[remote "origin"]
url = URL/TO/Project2
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "Path/To/Submodule2"]
url = ######
[submodule "Path/To/Subodule2"]
url = ######
and in Project2, there was a file called .gitmodules:
[submodule "Path/To/Submodule1"]
path = Path/To/Submodule1
url = ######
branch = master
[submodule "Path/To/Submodule2"]
path = Path/To/Submodule2
url = ######
branch = master
So what to do:
Part II: Solution
- Ensure that submodules have the same folder name
Project1 (not a repo)
|___ Submodule1
|___ Submodule2
Then copy Project2/.gitmodules
to Project1/
Then copy the content submodule part of Projec2's .git/conif
to Project1's .git/config
that it basically look like Project2, but not overriding [remote "origin"]:
And my Project1's .git/config
like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[submodule]
active = .
[remote "origin"]
*url = URL/TO/Project1*
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "Path/To/Submodule2"]
url = ######
[submodule "Path/To/Subodule2"]
url = ######