1

I have a question regarding submodules in git:

I have a repository "SuperRepo" with submodule "A". Submodule "A" points to branch "develop" in .gitmodules. If I create a new branch in "SuperRepo", let's assume "fancyFeature", I sometimes change the branch of submodule "A" to a new branch with the same name. This is not always the case, but sometimes.

I can submit this change and the submodule points to a commit on branch "fancyFeature". But since .gitmodules still points to develop, I always have unattached heads (at least that's my explanation). Is there any way to change the branch of a subrepo without having a detached head?

Thanks for your help!

P.S.:If I change the .gitmodules to "fancyFeature", I do not get detached heads. That's where my conclusion comes from...

  • Possible duplicate of [Why is my GIT Submodule HEAD detached from master?](https://stackoverflow.com/questions/18770545/why-is-my-git-submodule-head-detached-from-master) – phd Mar 19 '19 at 01:38
  • https://stackoverflow.com/search?q=%5Bgit-submodules%5D+detached+head – phd Mar 19 '19 at 01:38
  • Thanks for your answer. But the linked answers refer to repos without the change in subrepo branches. I do not have detached heads, if I stay on the same branch in my submodules. The tracking information is not updated, if I checkout a new branch. – user10228576 Mar 19 '19 at 07:50

1 Answers1

0

git submodules are using detached HEADs by default and there is no direct support to change this.

But you can:

  • set submodule.XXX.branch (in `.gitmodules) to the branch

  • go to the HEAD of this branch with

    git submodule update
    

    This command implies --checkout by default.

  • create the branch manually and use

    git submodule update --rebase
    git submodule update --merge
    

    This will keep your branch.

You can override the default update method by setting submodule.XXX.update to rebase, merge or checkout.

You can add --remote to fetch the submodule branch and merge/rebase/checkout this commit.

To create branches you can iterate through submodules like

git submodule foreach 'git -B feature-branch || :`
ensc
  • 6,704
  • 14
  • 22
  • Thanks a lot. My current workaround is to change the .gitmodules and run: `#!/bin/sh set -e git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | while read path_key path do url_key=$(echo $path_key | sed 's/\.path/.url/') url=$(git config -f .gitmodules --get "$url_key") git submodule add $url $path done` – user10228576 Mar 19 '19 at 07:52