Problem solve Get help with specific problems with your technologies, process and projects.

How to add git submodules with ease example

There are a number of ways to add Git submodules to an existing repository. You can clone from GitLab, you and do a Git submodule add from GitHub, or you can create all of your repositories and submodules manually. In this tutorial, we will focus on the later approach. That is, we will provide a Git submodules add example that doesn’t require any integration with GitHub or the need for any repositories to pre-exist. It’s a simple, bare bones approach to learning how the Git submodule add command works, and it’s the best way to learn how submodules in Git work.

How to add Git submodules

The steps to add Git submodules are:

  1. Create a repository with the git init command. This will be the parent module/repository.
  2. Add files and perform at least one commit to the parent repository before you add Git submodules
  3. Add a new subfolder in the Git repository
  4. In the new subfolder perform a git init command
  5. Add files and perform at least one commit on the repository in the subfolder
  6. From the root of the parent module, issue a git submodule add command and specify the path to the subfolder
  7. Perform a git status and verify the parent repository contains a file named .gitmodules
  8. Add the .gitmodules file to the index and then perform a commit.

The git submodule add command create a .gitmodules file.

If these steps complete without error, you have successfully performed a git submodule add.

Git submodule add commands

The actual commands used in the example are as follows:

submodule@example:~$ mkdir surface
submodule@example:~$ cd surface
submodule@example:~$ git init
submodule@example:~$ touch destroyer.html
submodule@example:~$ git add .
submodule@example:~$ git commit -m "Add surfae fleet"
submodule@example:~$ mkdir submarines
submodule@example:~$ cd submarines
submodule@example:~$ git init
submodule@example:~$ touch nuclear.html
submodule@example:~$ git add .
submodule@example:~$ git commit -m "Add submarine to fleet"
submodule@example:~$ cd ..
submodule@example:~$ git submodule add ./submarines
submodule@example:~$ git init
submodule@example:~$ git add .
submodule@example:~$ git commit -m "Add the submodule submarines"

How to add submodules in GitLab

If you use GitLab as your repository of choice, the following video demonstrates how to do a Git submodule add with GitLab.

Further git submodule add examples

This Git submodule add example was performed locally by initializing all of the required repositories. To see how to add a submodule through a clone, you might want to view either the GitHub submodule example or the GitLab example.

 

 

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close