top of page

Submodules

Objective: The objective of this assignment is to learn how to work with Git submodules. Submodules allow you to include external Git repositories as dependencies within your own Git repository. This assignment will help you practice adding, updating, and cloning submodules.


Your project relies on external libraries or subprojects. You need to manage these dependencies using Git submodules to keep them up to date.


Instructions:


Part 1: Repository Setup


1. Create a new directory on your computer for this assignment and name it "GitSubmodulesAssignment."


2. Navigate to the "GitSubmodulesAssignment" directory in your terminal or command prompt.


3. Initialize a new Git repository in this directory.


Part 2: Adding a Submodule


1. Use the following Git command to add a submodule to your repository. Replace `<submodule-repo-url>` with the URL of the Git repository you want to add as a submodule.


   

   git submodule add <submodule-repo-url> <submodule-path>

   


   - `<submodule-repo-url>`: The URL of the external Git repository.

   - `<submodule-path>`: The path where the submodule will be added within your repository.


2. Commit the changes to your main repository.


Part 3: Cloning a Repository with Submodules


1. Clone a Git repository that contains submodules using the following command. Replace `<repository-url>` with the URL of the repository.


   

   git clone --recurse-submodules <repository-url>

   


   This command ensures that submodules are also cloned.


Part 4: Updating Submodules


1. Use the following Git commands to update submodules within your repository:


   

   git submodule init

   git submodule update

   


   These commands initialize and update submodules to their latest commits.


Part 5: Working with Submodules


1. Explore the contents of the submodule directory within your main repository. You can make changes, commit them, and push them to the submodule's repository.


2. Use the following Git commands to fetch and merge changes from the submodule's repository into your main repository:


   

   cd <submodule-path>

   git pull origin <branch-name>

   cd ..

   git commit -am "Update submodule: <submodule-name>"

   


   Replace `<submodule-path>` with the path to the submodule within your repository and `<branch-name>` with the submodule's branch name.


Part 6: Viewing Submodule Status


1. Use the following Git command to view the status of submodules within your repository:


   

   git submodule status

   


   This command displays information about each submodule, including the commit hash and the path.

Solutions

Part 1: Repository Setup



# Create a directory

mkdir GitSubmodulesAssignment


# Navigate to the directory

cd GitSubmodulesAssignment


# Initialize a Git repository

git init



Part 2: Adding a Submodule



# Add a submodule

git submodule add <submodule-repo-url> <submodule-path>


# Commit the changes

git commit -m "Add submodule: <submodule-name>"



Part 3: Cloning a Repository with Submodules



# Clone a repository with submodules

git clone --recurse-submodules <repository-url>



Part 4: Updating Submodules



# Initialize submodules

git submodule init


# Update submodules

git submodule update



Part 5: Working with Submodules



# Change directory to submodule

cd <submodule-path>


# Pull changes from submodule's repository

git pull origin <branch-name>


# Change directory back to the main repository

cd ..


# Commit changes to the main repository

git commit -am "Update submodule: <submodule-name>"



Part 6: Viewing Submodule Status



# View submodule status

git submodule status


bottom of page