![]() |
Adding submodules in Git is a great way to include and manage external repositories within your own project. Sometimes, you might need to add a specific branch or tag as a submodule. Here’s a guide on how to do that efficiently. What is a Git Submodule?A Git submodule is a repository inside another repository. It allows you to keep another Git repository as a subdirectory of your repository and manage it as a separate entity. This is particularly useful for including libraries or other dependencies that you want to keep track of independently. Approach 1: Adding a submodule with Specific BranchWhen adding a submodule, you can specify a branch to track using the ‘-b’ option. Step 1: Navigate to your project directory: cd your_project_directory
![]() This is my project directory Step 2: Add the submodule with the specified branch: git submodule add -b branch-name https://github.com/rohitchoudhray14/repository.git path/to/submodule
![]() How To Specify A Branch/Tag When Adding a Git Submodule Here :
Approach 2: Adding a Submodule with a Specific TagGit does not directly support checking out tags for submodules during the add command, but you can achieve this with a few extra steps. Step 1: Run this command: git submodule add https://github.com/username/repository.git path/to/submodule
![]() How To Specify A Branch/Tag When Adding a Git Submodule Step 2: Now navigate to submodule directory: cd path/to/submodule
![]() cd submodules/blackcoffer Step 3: Checkout the desired tag: git checkout tags/tag-name
If the tag are not added first then you will see ![]() Here article tag was not found in the repositery. So will will create first then checkout tag. ![]() article tag has been created Now we checkout the tag…. ![]() git checkout tags/article Step 4: Return to the main project directory and commit the changes: cd ../../
git add path/to/submodule
git commit -m "Checked out tag tag-name for submodule"
![]() cd ../../git add submodules/blackcoffergit commit -m “Checked out tag article for submodule” Example:git submodule add https://github.com/example/example-repo.git submodules/example-repo
cd submodules/example-repo
git checkout tags/v1.0.0
cd ../../
git add submodules/example-repo
git commit -m "Checked out tag v1.0.0 for submodule"
Approach 3: Updating an Existing Submodule to Track a Specific BranchIf you have an existing submodule that you want to update to track a different branch, you can do so by modifying the submodule’s configuration. Step 1: Navigate to submodule directory: cd path/to/submodule
![]() cd submodules/blackcoffer Step 2: Checkout the desired branch: git checkout branch-name
![]() git checkout main Step 3: Update the submodule to track the branch: git branch --set-upstream-to=origin/branch-name
![]() git branch –set-upstream-to=origin/main Step 4: Return to the main project directory and commit the changes: cd ../../
git add path/to/submodule
git commit -m "Updated submodule to track branch branch-name"
![]() How To Specify A Branch/Tag When Adding a Git Submodule Examplecd submodules/example-repo
git checkout develop
git branch --set-upstream-to=origin/develop
cd ../../
git add submodules/example-repo
git commit -m "Updated submodule to track branch develop"
Approach 4: Switching a Submodule to a Different Tag or BranchTo switch an existing submodule to a different tag or branch follow these steps. Step 1: Navigate to the submodule directory: cd path/to/submodule
![]() cd submodules/blackcoffer Step 2: Fetch all tags and branches : git fetch
![]() How To Specify A Branch/Tag When Adding a Git Submodule Step 3: Checkout the desired branch or tag: git checkout branch-name-or-tag-name
For tag name: ![]() git checkout article For branch: ![]() git checkout main Step 4: Return to the main project directory and commit the changes: cd ../../
git add path/to/submodule
git commit -m "Switched submodule to branch/tag branch-name-or-tag-name"
![]() How To Specify A Branch/Tag When Adding a Git Submodule Examplecd submodules/example-repo |
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |