Git

How to Change the Remote Repository for a Git Submodule?

A submodule in Git can add one Git repository within another repository. This allows users to maintain separate projects within a larger project and easily share code between them. A project may have multiple submodules; each module represents a different project component. The submodule can be updated independently of the main project, allowing developers to make changes and test them before merging them into the main project.

This write-up will provide the method for changing the GitHub repository for a Git submodule.

How to Change the GitHub Repository for a Git Submodule?

To change the GitHub repository for a submodule in Git:

  • First, navigate to the desired local repository that contains the submodule.
  • Then, switch to the submodule and check its remote URL.
  • Next, move back to the parent repository and execute the “git submodule set-url <submodule-name> <new-remote-url>” command to change the submodule’s remote URL.
  • Finally, navigate to the submodule again and verify the new remote URL.

Step 1: Move to Local Repository
First, enter the “cd” command along with the particular repository path that contains a submodule and switch to it:

$ cd "C:\Git\ReposB"

Step 2: List Repository Content
Next, display the content of the current directory:

$ ls

It can be observed that the working repository contains one submodule named “subMod”:

Step 3: Navigate to Submodule
Then, utilize the following command along with the submodule name and move to it:

$ cd subMod

Step 4: Check Remote URL
Execute the below-listed command to check the remote URL of the submodule:

$ git remote -v

According to the given output, the submodule is linked to the remote repository with the given remote URL:

Step 5: Move Back to Parent Repository
Switch back to the present repository using the following command:

$ cd ..

Step 6: Change Remote URL of Submodule
Now, run the “git submodule set-url” command along with the module name and new remote URL:

$ git submodule set-url subMod https://github.com/laibayounas/newRepo.git

Here, “subMod” is the submodule name:

Step 7: Switch to Submodule
Navigate to the submodule again to view new changes:

$ cd subMod

Step 8: Verify Remote URL
Lastly, ensure whether the Git submodule’s remote repository has changed or not by running the given-provided command:

$ git remote -v

It can be seen that the remote repository of the submodule has been changed with the new URL:

We have explained the procedure for changing the GitHub repository for a submodule.

Conclusion

To change the GitHub repository for a Git submodule, first, move to the desired local repository containing the submodule. Then, run the “git submodule set-url <submodule-name> <new-remote-url>” command. Next, navigate to the submodule and ensure the new remote repository by typing out the “git remote -v” command. This article demonstrated the method of changing the GitHub repository for a Git submodule.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.