Introduction

Adding a submodule in Git allows you to include and track another Git repository at a specific commit within your repository. This is particularly useful for including libraries, frameworks, or other shared components in your projects.

Lazygit

Step 1: Open Lazygit in Your Repository

  • Launch Lazygit in your main Git repository where you want to add the submodule.

Step 2: Access Submodule Menu

  • Navigate to the Submodule menu in Lazygit. This might require a specific key binding or navigating through the interface, depending on your Lazygit configuration.

Step 3: Add a New Submodule

  • Within the Submodule menu, find the option to add a new submodule.
  • Enter the URL of the repository you wish to add as a submodule.
  • Specify the path within your repository where the submodule should be placed.

Step 4: Initialize and Update the Submodule

  • After adding, initialize and update the submodule to ensure it’s correctly set up and the content is fetched.

Step 5: Commit the Submodule Addition

  • Commit the addition of the submodule to your repository. This will include the .gitmodules file and the submodule directory.

Basic Terminal: Adding a Submodule

Step 1: Navigate to Your Git Repository

  • Open a terminal and navigate to the root directory of your main Git repository.

Step 2: Add the Submodule

  • Use the git submodule add command followed by the URL of the repository and the path where you want to place the submodule:

    git submodule add <repository-url> <path>

    For example:

    git submodule add https://github.com/example/lib.git external/lib

Step 3: Initialize and Update the Submodule

  • After adding the submodule, initialize and update it:

    git submodule init
    git submodule update

Step 4: Commit the Changes

  • Commit the changes to your repository, including the addition of the .gitmodules file and the submodule’s content:

    git add .
    git commit -m "Added submodule at <path>"

Conclusion

Adding a submodule in Git is a powerful feature for managing dependencies and shared components. Whether using Lazygit or the command line, it’s important to understand the implications of submodules in your project’s structure and version control. Be sure to commit all changes related to the submodule to track its integration into your repository effectively.