Git

How to Change an Existing Submodule’s Branch?

A submodule permits developers to include one Git directory as a subdirectory of another repository. It contains its history, branches, and tags. When developers initially add the submodule in their projects, it adds with its default branch. However, sometimes, you may want to change their submodule’s branch to their desired branch. In this situation, Git allows you to change the existing branch of the submodule.

This write-up will explain the method to change an existing branch submodule in Git.

How to Change an Existing Submodule’s Branch in Git?

To change an existing branch of the submodule, check out the below-listed steps:

  • Redirect to the local directory.
  • Switch to the submodule.
  • Change the branch using the “git checkout <new-branch>” command.
  • Move back to the parent repository.
  • Track and commit newly added changes.

Step 1: Move to Local Repository

First, write out the “cd” command with the desired repository path and switch to it:

$ cd "C:\Git\ReposC"

Step 2: List Local Repository Content

Then, use the below-provided command to list the content of the working repository:

$ ls

It can be observed that the repository contains two files and one submodule named “demo/”:

Step 3: Navigate to Submodule

Now, redirect to the submodule by typing out the “cd” command along with the submodule name:

$ cd demo

Step 4: Check Submodule’s Branch

Check the current branch of the submodule with the help of the below-provided command:

$ git branch

According to the following output, the “main” branch is the submodule’s current working branch:

Step 5: Change Submodule’s Branch

Utilize the “git checkout” command and specify the desired branch name to change the branch:

$ git checkout master

As a result, the “main” branch will be changed to the “master” branch:

Step 6: Move Back to Parent Repository

Then, switch back to the parent repository using the following command:

$ cd ..

Step 7: Add Changes to Git Index

Next, use the “git add” command along with the “.” symbol to add all the changes to the Git staging area:

$ git add .

Step 8: Commit Changes

Lastly, save all the added changes through the “git commit” command and specify the commit message:

$ git commit -m "Changed submodule's branch"

We have explained the procedure for changing an existing branch of the submodule.

Conclusion

To change an existing submodule’s branch, first, navigate to the local repository that has the submodule. Then, navigate to the submodule. After that, utilize the “git checkout <new-branch>” command to change the branch. Next, move back to the parent repository, track, and commit new changes. This write-up explained the method to change an existing submodule’s branch in Git.

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.