0

We're starting to use git submodules and I'm trying to provide some chrome for my developers.

I know git stores the SHA of the submodule so it takes a commit, so that means if a developer wants to update a submodule dependency, they do it by CDing in and getting latest, then committing the change in the outer dir.

But the problem is that by default git submodules have detached heads. Is there way to configure a git submodule within the repo to always track a certain remote branch? And commands to change this remote tracking branch?

I'd like to be able to write a quick "update my submodule and commit" script, or a quick "switch my submodule's branch from the dev to a release branch". The fact that we're switching branches means I can't just hardcode the branch name everywhere. Does git store this in a decent location, or am I going to have to roll my own .submoduleconfig thingy for my scripts to read?

I have searched for this before and most of the similar questions are how to make it auto-track latest (which git does not support) and alternately how to manually push the submodule to the latest of a specific branch you give it, not just "you're already tracking this branch, get latest".

Pxtl
  • 880
  • 8
  • 18
  • Possible duplicate of [How an I specify a branch/tag when adding a submodule?](https://stackoverflow.com/questions/1777854/how-an-i-specify-a-branch-tag-when-adding-a-submodule) – phd Oct 23 '18 at 20:48
  • https://stackoverflow.com/search?q=%5Bgit-submodules%5D+detached+head – phd Oct 23 '18 at 20:48

1 Answers1

0

On further testing, I found out the problem is that git is picky about parameter ordering, and it very confusing on this one.

To add a submodule that is set to follow a branch, you must use

git submodule add --branch $branchName $remoteURI $modulePath

The important thing above is that the --branch can't be at the end. I have no idea why.

This creates a .gitmodules file which includes

branch = $branchName

which many other answers point out. But what they don't make clear is that this is separate from the normal git branch tracking process. When you pull down your submodule, it will not be connected to a branch if you CD into the submodule folder, so it looks like the branch = dev thing failed and is worthless.

However, the actual workflow is that git has a completely different mechanism for updating submodules to latest along their branches, if you use branch = $branchName thing. Very confusing.

Instead of going

cd $modulePath
git pull

like nearly every instruction says, you use a completely different command to update your submodules.

git submodule update --remote

(this can be filtered to a specific submodule if you like).

And after you do this, your submodule will be at the HEAD of that branch but if you cd into $modulePath, it will still say just a commit hash instead of the branch name because it's manipulating this from the outside using the submodule system instead of the normal git tracking workflow.

Pxtl
  • 880
  • 8
  • 18