4

I've added 2 submodules to a project, subA and subB, in externals/subA and externals/subB.

Today another team member committed his code and when it is pulled, both subA and subB show detached head status when using git status within externals/subA and externals/subB.

I did git submodule update first and that reported no errors. I tried then git submodule init and git submodule update again but it did not change.

How can we get the submodules back to be in sync? What causes this for the submodules? This is first time problem has arised since we began. Thanks.

1 Answers1

4

A submodule is by definition checked out in detached HEAD: it represents a specific SHA1 recorded as a gitlink in the parent repo index.

See "git submodule update" in order to make sure a submodule is tracking a branch:

# add submodule to track master branch
git submodule add -b master [URL to Git repo];

# update your submodule
git submodule update --remote 
# or (with rebase)
git submodule update --rebase --remote

Note: as shown here, any git submodule update command would automatically detach the HEAD, even if the submodule is configured to follow a branch.

As test add git config submodule.<name>.update merge, because by default, an update checks out the commit (detached HEAD)


user859375 adds in the comments:

The git submodule is detached in the init stage itself when running command "git submodule update --init --recursive".

I understand git refers and check-out particular commit of gitsubmodules.

So, we created a script to run "git submodule foreach git checkout master" and "git submodule foreach git pull origin master" among other things.

This way we're able to keep the attached head state when setting up repo in local machine.
And yes we run "git submodule update --remote --merge" when we need to update submodules to reflect changes in remote.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • It still does bit track branch – user859375 May 09 '20 at 17:28
  • @user859375 Do you mean the tracking branch part is not working? – VonC May 09 '20 at 17:33
  • The "git status" still shows head detached so yes I suppose – user859375 May 09 '20 at 17:37
  • @user859375 After reading https://stackoverflow.com/a/55570998/6309 (that you have also consulted), you should try `git config submodule..update merge`, and then see if the HEAD is still detached after a `git submodule update --rebase --remote` (with a `git status` done within the submodule, not from the parent repo) – VonC May 09 '20 at 19:34
  • The git submodule is detached in the init stage itself when running command "git submodule update --init --recursive". I understand git refers and check-out particular commit of gitsubmodules. So, we created a script to run "git submodule foreach git checkout master" and "git submodule foreach git pull origin master" among other things. This way we're able to keep the attached head state when setting up repo in local machine. And yes we run "git submodule update --remote --merge" when we need to update submodules to reflect changes in remote. Thanks for the response and confimation – user859375 May 16 '20 at 09:20
  • @user859375 OK. I have included your comment in the answer for more visibility. – VonC May 16 '20 at 09:24