If you have a Git repository with submodules, you’ll likely be surprised to find out that when you clone the repository, it does not bring down all of the branches and code for the associate submodules. A few extra steps need to be taken.
Git clone with submodules
The list of steps required to clone a Git repository with submodules is:
- Issue a git clone command on the parent repository.
- Issue a git submodule init command.
- Issue a git submodule update command.
When these three steps are complete, both the parent repository and all submodules will be available locally, and files from the master branch of the parent repository and the submodule will be available in their respective git working trees.
Git init and update alternative
There is actually an alternative to going through these three steps. You can use the –recurse-submodules switch on the clone. This approach, shown below, might be easier.
git clone --recurse-submodules https://github.com/cameronmcnz/surface.git
Git submodule clone
The commands issued to clone the git repository and all submodules are:
[email protected]:~$ git clone https://github.com/cameronmcnz/surface.git [email protected]:~$ git submodule init [email protected]:~$ git submodule update
The clone operation will obviously happen after multiple repositories have been created and submodules added to them. For more information on how to perform other git submodule operations, check out our full listing of “git submodule how to” tutorials.
Become a Git power user
Want to become a Git power user? Take a look at the following Git articles and tutorials
- How to do a Git clean up of branches and commits
- Learn to rebase onto master and rebase from the master branch
- Squash all Git commits on a branch down to one
- Shelve your changes with Git stash pop and apply
- Easily explain the Git vs GitHub difference
- Add a shallow git clone of depth 1 do your Jenkins jobs
- Set up a local Git server with a bare Git repo