I have this configuration for submodule in .gitmodules file:
[submodule "sub"]
shallow = true
branch = master
path = sub
url = https://path/to/repo.git
Now I want when someone clones my repo and then runs these commands:
git submodule init
git submodule update
Is to get shallow master branch of submodule. But what happens is it does not checkout to master branch. It will always get to detached head, so then I need to manually run git checkout master. So instead of just those two commands, user needs to run one additional.
I looked into this: Why is my GIT Submodule HEAD detached from master?
But any advice that was on accepted answers, does not seem to help: I added branch I want in .gitmodules file, I added remote upstream to be master (this only works for already cloned/updated repository after I had to checkout to master myself).
So is this intended to always get detached HEAD if someone clones my repository and wants to set up submodule?
Yes, you're right. The top answer maded by user mkungla from Why is my GIT Submodule HEAD detached from master? is nonsense.
Adding a
branchoption in.gitmoduleis NOT related to the detached behavior of submodules at all.From
git submodule --help, HEAD detached is the default behavior ofgit submodule update --remote.First, there's no need to specify a branch to be tracked.
origin/masteris the default branch to be tracked.Why
So why is HEAD detached after
update? Because the default behavior ofsubmodule.$name.updateischeckout.How
If you want the submodule merged with remote branch automatically, use
--mergeor--rebase.All you need to do is,
There's also an option to make
--mergeor--rebaseas the default behavior ofgit submodule update, by settingsubmodule.$name.updatetomergeorrebase.Here's an example about how to config the default update behavior of submodule update in
.gitmodule.My whole answer is based on the manual.
git submodule --help.