Submodules can be added into a repo as independent repos, so that each
submodule has a .git
-directory of its own:
[submodule-test] $ mkdir super-repo
[submodule-test] $ cd super-repo/
[super-repo] $ mkdir sub-repo
[super-repo] $ cd sub-repo/
[sub-repo] $ git init
Initialized empty Git repository in /home/foobar/tmp/submodule-test/super-repo/sub-repo/.git/
[sub-repo] $ touch foo.txt
[sub-repo] $ git add foo.txt
[sub-repo] $ git commit -m "* initial commit"
[main (root-commit) 60c3e14] * initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 foo.txt
[sub-repo] $ cd ..
[super-repo] $ git init
Initialized empty Git repository in /home/foobar/tmp/submodule-test/super-repo/.git/
[super-repo] $ git submodule add ./sub-repo sub-repo
Adding existing repo at 'sub-repo' to the index
[super-repo] $ git commit -m "* initial commit"
[main (root-commit) 826f205] * initial commit
2 files changed, 4 insertions(+)
create mode 100644 .gitmodules
create mode 160000 sub-repo
[super-repo] $ file sub-repo/.git
sub-repo/.git: directory
However, if I now clone the super repo, submodules will no longer be
independent repos, because their .git
will be a file with a link to
the super repo. Below the flags for git clone
are selected to
- allow cloning within a file system
- reflect my real-world situation, where I have lots of remote submodules within remote submodules. (It is not feasible to go through these one-by-one, so I need something automated. The submodules also have differently named default branches.)
[super-repo] $ cd ..
[submodule-test] $ mkdir clone-of-super
[submodule-test] $ cd clone-of-super/
[clone-of-super] $ git -c protocol.file.allow=always clone --recurse-submodules --remote-submodules ../super-repo
Cloning into 'super-repo'...
done.
Submodule 'sub-repo' (/home/foobar/tmp/submodule-test/clone-of-super/../super-repo/sub-repo) registered for path 'sub-repo'
Cloning into '/home/foobar/tmp/submodule-test/clone-of-super/super-repo/sub-repo'...
done.
Submodule path 'sub-repo': checked out '60c3e1457a183d14a6af4ba9472e99d99e12de94'
[clone-of-super] $ cd super-repo/
[super-repo] $ file sub-repo/.git
sub-repo/.git: ASCII text
[super-repo] $ cat sub-repo/.git
gitdir: ../.git/modules/sub-repo
So, the question is: How can a repo be cloned so that submodules are cloned as independent repos, with .git
-directories of their own?