How to Clone a Remote Repository With Submodules in Git

Kevin Amayi Feb 15, 2024
  1. Clone a Remote Repository With Submodules in Git
  2. Create a Submodule and Push to a Remote Repository Before Cloning in Git
How to Clone a Remote Repository With Submodules in Git

This article will discuss how to clone a remote Git repository with submodules. We will also create a submodule and push it to a remote repository before cloning it.

Clone a Remote Repository With Submodules in Git

We use the following command to clone our repository together with the submodules.

git clone --recurse-submodules -j8 git@github.com:KEVINAMAYI/AkanNameGenerator.git

Output:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.86 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule 'testfolder' (git@github.com:KEVINAMAYI/AkanNameGenerator.git) registered for path 'testfolder'
Cloning into '/home/kevin/tqt/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.55 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule path 'testfolder': checked out '3300a2aa47ef2c490c19541c6907117511eabe08'

Create a Submodule and Push to a Remote Repository Before Cloning in Git

Before cloning a repository, we will first add a submodule with the name testfolder to an already existing local repository and then push the changes to a remote repository.

<!-- this commands intializes a submodule with the contents of a remote repo-->
git submodule add <your remote repo url> <name of submodule>

git submodule add git@github.com:KEVINAMAYI/AkanNameGenerator.git testfolder

Output:

Cloning into '/home/kevin/tst/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 105, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 105 (delta 3), reused 0 (delta 0), pack-reused 94
Receiving objects: 100% (105/105), 2.38 MiB | 2.06 MiB/s, done.
Resolving deltas: 100% (28/28), done.

Next, we check our new files. We should see an extra testfolder in the list.

ls

Output:

css  images  index.html  js  LICENSE  README.md  testfolder  vendor

Then, we will commit the changes we have just made.

git commit -m "Added the submodule to the project."

Output:

"Added the submodule to the project."
[main 500a12a] Added the submodule to the project.
2 files changed, 4 insertions(+)
create mode 100644 .gitmodules
create mode 160000 testfolder

We will push our changes to a remote repository using this command.

git push

Output:

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 429 bytes | 429.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:KEVINAMAYI/AkanNameGenerator.git
3300a2a..500a12a  main -> main

Now our remote repository has the submodule testfolder.

Clone a Repository with Submodules

We will clone our repository together with the submodules.

git clone --recurse-submodules -j8 git@github.com:KEVINAMAYI/AkanNameGenerator.git 

Output:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 108, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94
Receiving objects: 100% (108/108), 2.38 MiB | 1.86 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule 'testfolder' (git@github.com:KEVINAMAYI/AkanNameGenerator.git) registered for path 'testfolder'
Cloning into '/home/kevin/tqt/AkanNameGenerator/testfolder'...
remote: Enumerating objects: 108, done.        
remote: Counting objects: 100% (14/14), done.        
remote: Compressing objects: 100% (11/11), done.        
remote: Total 108 (delta 4), reused 3 (delta 1), pack-reused 94        
Receiving objects: 100% (108/108), 2.38 MiB | 1.55 MiB/s, done.
Resolving deltas: 100% (29/29), done.
Submodule path 'testfolder': checked out '3300a2aa47ef2c490c19541c6907117511eabe08'

Related Article - Git Clone