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:
Step 2: List Local Repository Content
Then, use the below-provided command to list the content of the working repository:
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:
Step 4: Check Submodule’s Branch
Check the current branch of the submodule with the help of the below-provided command:
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:
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:
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:
Step 8: Commit Changes
Lastly, save all the added changes through the “git commit” command and specify the commit message:
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.