0 votes
1 view
in Devops and Agile by (16.9k 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 Answer

0 votes
by (22.3k 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.