EDIT:
See @Simba's Answer for a Correct Solution
.submodule.<name>.update is what you want to change, see the docs - default checkout
submodule.<name>.branch specify remote branch to be tracked - default master
OLD RESPONSE:
Personally, I hate answers here that are aimed at external links that may stop working with time, and check my answer here (if the question does not repeat itself) - moving to a question that covers the topic between the lines of another topic, but in general it’s equal to: "I I don’t answer, read the documentation. "
So, back to the question: why is this happening?
Situation you described
After receiving the changes from the server, my submodule head was disconnected many times from the main branch.
This is a common case when someone does not use submodules too often or just started with submodules. I believe that I correctly state that we were all there at some point when our HEAD submodule disconnected.
- Cause. Your submodule does not track the correct branch (default wizard).
Solution: make sure your submodule is tracking the correct branch
$ cd <submodule-path>
- Cause. Your parent repository is not configured to track the submodule branch.
Decision. Make your submodule track its remote branch by adding new submodules using the following two commands.- First you tell git to track your
<branch> remote. - you tell git to rebase or merge instead of fetching
- you tell git to update your submodule from the remote.
$ git submodule add -b <branch> <repository> [<submodule-path>] $ git config -f .gitmodules submodule.<submodule-path>.update rebase $ git submodule update --remote
- If you haven't added your submodule like this yet, you can easily fix this:
- First, you want to make sure that the branch you want to track is marked in your submodule.
$ cd <submodule-path> $ git checkout <branch> $ cd <parent-repo-path>
In normal cases, you have already fixed your DETACHED HEAD, as it was due to one of the configuration problems above.
fix DETACHED HEAD when .update = checkout
$ cd <submodule-path>
But if you manage to make some changes locally for the submodule and the committed changes, send them to the remote computer, and then when you have done the “git checkout”, Git will notify you:
$ git checkout <branch> Warning: you are leaving 1 commit behind, not connected to any of your branches: If you want to keep it by creating a new branch, this may be a good time to do so with:
It is recommended that you create a temporary branch, and then you can simply merge these branches, etc. However, in this case, I personally would use only git cherry-pick <hash> .
$ git cherry-pick <hash>
Although there are a few more cases where you can put your submodules in the DETACHED HEAD state, I hope that now you understand a little more how to debug your specific case.