Horje
How To Specify A Branch/Tag When Adding a Git Submodule?

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 Branch

When 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  
Screenshot-2024-06-12-121652

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
  • branch-name: The branch you want the submodule to track.
  • https://github.com/username/repository.git: The URL of the submodule repository.
  • path/to/submodule: The path where you want to add the submodule in your project.
Screenshot-2024-06-12-123104

How To Specify A Branch/Tag When Adding a Git Submodule

Here :

  • -b main : (Branch name )
  • Repository : https://github.com/rohit-choudhary14/Zillow.git
  • Path of submodule : submodules/blackcoffer( Create as per requirement)

Approach 2: Adding a Submodule with a Specific Tag

Git 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
Screenshot-2024-06-12-124129

How To Specify A Branch/Tag When Adding a Git Submodule

Step 2: Now navigate to submodule directory:

cd path/to/submodule
Screenshot-2024-06-12-124332

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

Screenshot-2024-06-12-125315

Here article tag was not found in the repositery.

So will will create first then checkout tag.

Screenshot-2024-06-12-125537

article tag has been created

Now we checkout the tag….

Screenshot-2024-06-12-125737

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"
Screenshot-2024-06-12-130019

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 Branch

If 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
Screenshot-2024-06-12-130400

cd submodules/blackcoffer

Step 2: Checkout the desired branch:

git checkout branch-name
Screenshot-2024-06-12-130545

git checkout main

Step 3: Update the submodule to track the branch:

git branch --set-upstream-to=origin/branch-name
Screenshot-2024-06-12-130716

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"
Screenshot-2024-06-12-130938

How To Specify A Branch/Tag When Adding a Git Submodule

Example

cd 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 Branch

To switch an existing submodule to a different tag or branch follow these steps.

Step 1: Navigate to the submodule directory:

cd path/to/submodule
Screenshot-2024-06-12-131157

cd submodules/blackcoffer

Step 2: Fetch all tags and branches :

git fetch
Screenshot-2024-06-12-131331

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:

Screenshot-2024-06-12-131526

git checkout article

For branch:

Screenshot-2024-06-12-131700

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"
Screenshot-2024-06-12-131823

How To Specify A Branch/Tag When Adding a Git Submodule

Example

 cd submodules/example-repo
git fetch
git checkout v1.1.0
cd ../../
git add submodules/example-repo
git commit -m "Switched submodule to tag v1.1.0"



Reffered: https://www.geeksforgeeks.org


Git

Related
How To Skip Git Commit Hooks? How To Skip Git Commit Hooks?
Handling Git Pull Without Specifying a Warning Handling Git Pull Without Specifying a Warning
How To Fix Error: Permission denied (publickey)? How To Fix Error: Permission denied (publickey)?
How to Create Branch From a Previous Commit Using Git? How to Create Branch From a Previous Commit Using Git?
How To Configure The Core (Commit) And Sequence Git Editors? How To Configure The Core (Commit) And Sequence Git Editors?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17