Add a Submodule

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Submodules are a way to include one Git repository as a subdirectory of another Git repository. This can be useful when you want to include a third-party library or a shared codebase in your project. In this lab, you will learn how to add a submodule to a Git repository.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/add("`Stage Files`") subgraph Lab Skills git/add -.-> lab-12697{{"`Add a Submodule`"}} end

Add a Submodule

Your task is to add a new submodule to a Git repository. You will need to use the git submodule add command to add the submodule from an upstream repository to a local directory in your repository. The syntax for the command is as follows:

git submodule add <upstream-path> <local-path>
  • <upstream-path> is the URL or path to the upstream repository that you want to add as a submodule.
  • <local-path> is the path where you want to store the submodule in your local repository.

Suppose you have a Git repository named my-project and you want to add a submodule from the Git repository https://github.com/labex-labs/git-playground.git to a directory named git-playground in your local repository. Here's how you can do it:

git init my-project
cd my-project
git submodule add https://github.com/labex-labs/git-playground.git ./git-playground

This is the result after completing the lab:

Summary

Adding a submodule to a Git repository can be useful when you want to include a third-party library or a shared codebase in your project. In this lab, you learned how to use the git submodule add command to add a new submodule to a Git repository. Remember to specify the upstream repository URL or path and the local directory where you want to store the submodule.

Other Git Tutorials you may like