Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k points)

How does git submodule add -b work?

After adding a submodule with a specific branch, a newly cloned repository (after git submodule update --init) will be at a specific commit, not the branch itself (git status on the submodule shows "Not currently on any branch").

I can't find any information on .gitmodules or .git/config about the submodule's branch or any specific commit, so how does Git figure it out?

Also, is it possible to specify a tag instead of a branch?

I'm using version 1.6.5.2.

1 Answer

0 votes
by (27.5k points)

Sub-modules are not on a branch. They are just a pointer to a particular commit of the sub-module's repository.

When someone else checks out your repository, or pulls your code, and performs some git sub-module update, the sub-module is checked out to that particular commit.

Say, you want to move the sub-module to a particular tag:

cd submodule_directory

git checkout v1.0

cd ..

git add submodule_directory

git commit -m "moved submodule to v1.0"

git push

Now let us say another guy from the developer team wants to have submodule_directory changed to that tag, all he has to do is,

git pull

git submodule update --init

The git pull command changes which commit their sub-module directory points to.  The git sub-module update command actually merges in the new code.

To get an overview of Git, you must read the Git Tutorial. Also, if you want to learn more about it, you can enroll in Git Training.

Related questions

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...