0

I have:

  • A main repository we call "MAIN"
  • A submodule repository we call "SUB"
  • In MAIN I add SUB as submodule
  • Both repos are in master branch

Now what I need to do is creating a branch in both MAIN and SUB, such that when I'm on this branch on MAIN I can switch to a particular branch also in SUB. It seems to be impossible.

What I did is I created a branch (called branch_sub) in SUB and pushed the new branch. Now in MAIN I created another branch (called branch_main) in MAIN and I modified both the .gitmodule and .git/config putting branch_sub as "branch" field, such that the submodule should point at that particular branch. Pushed the commit in the branch of MAIN.

Now if I do git submodule update, the SUB goes back to master branch and the original commit. Why? It's very stupid imo. If I switch manually to branch_sub, then do in MAIN git submodule update --remote, the SUB stays on the correct commit, but in a detached HEAD instead of the branch branch_sub :(.

Any tip? Is it possible what I need to do? It seems very basic to me but git submodule doesn't seem to support it easily.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • Possible duplicate of [How can I specify a branch/tag when adding a Git submodule?](https://stackoverflow.com/questions/1777854/how-can-i-specify-a-branch-tag-when-adding-a-git-submodule) – kowsky Jan 10 '19 at 13:10
  • Possible duplicate of [git submodule update](https://stackoverflow.com/questions/1979167/git-submodule-update) – phd Jan 10 '19 at 13:22
  • https://stackoverflow.com/search?q=%5Bgit-submodules%5D+switch+branch – phd Jan 10 '19 at 13:22
  • https://stackoverflow.com/questions/1899792/why-is-git-submodule-update-not-automatic-on-git-checkout – phd Jan 10 '19 at 13:22
  • @kowsky. It doesn't work as already written in my question. It only moves to the commit but in a detached head instead of checking out to the desired branch. – Michele Tuloski Furci Jan 10 '19 at 13:52
  • Oh yea, sorry. The same question was already asked in the question I linked in my answer: [git submodule is always detached, even when tracking branch](https://stackoverflow.com/questions/53889819/git-submodule-is-always-detached-even-when-tracking-branch) – kowsky Jan 10 '19 at 14:04

1 Answers1

0

What you want to achieve is not possible. Submodules always check out a certain commit, never a branch, and thus will always be in a detached HEAD state. The commit that is checked out may be determined by the branch set for the submodule, as you experienced. Also see this answer.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • So do I need to manually checkout to the desired branch in the submodules when making modification? For sure I'll forget and make modification from the detached head and lose the work :(. It's a pity submodules don't support a link to the branch instead of plain commit. – Michele Tuloski Furci Jan 10 '19 at 14:35
  • @MicheleTuloskiFurci: yes, enter the submodule and `git checkout ` to do work there. If you do forget, and a `git submodule update` forces your detached HEAD to an undesired commit, `cd` back into the submodule, use `git reflog` to find the desired commit, and fix the detached-HEAD-ness now that you are reminded. – torek Jan 10 '19 at 16:04