Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

Git submodule to track remote branch

I'm trying to use git submodules for aggregating 10+ repositories into one structure for easy development.

It is supposed to clone the module and checkout a branch. Instead, the module is checked out in detached head mode.

git clone [email protected]:org/global-repository.git
git submodule update —init
cd config-framework
git status

$git status
#HEAD detached at b932ab5
nothing to commit, working directory clean

gitmodules files seems to be okay

$cat .gitmodules 
[submodule "config-framework"]
        path = config-framework
        url = [email protected]:org/config-framework.git
        branch = MY_BRANCH

We want the MY_BRANCH branch to be checked out by default, rather than detached head. How do we achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Submodules are always checked out in a detached HEAD mode.

That is because a submodule will checkout the SHA1 stored in the special entry in the index of the parent repo.

Plus, if you want a submodule to follow the branch you have registered in the .gitmodules, you need:

git submodule update --init --remote

The --remote will make a git fetch, plus a checkout of the new HEAD.
Alas, even that checkout will be of a commit, not of the branch (since you have no local branch by default in a submodule), so... back to a detached HEAD mode.
See more at "Git submodules: Specify a branch/tag".

You can try (not tested) a:

git submodule foreach 'git checkout -b $(git config -f /path/to/parent/repo/.gitmodules --get submodule.$path.branch)'

I take advantage of the fact git submodule foreach has access to '$path', the name of the submodule directory relative to the superproject.


There was an attempt to specify a branch for a submodule to be automatically checked out in (commit 23d25e4 for Git 2.0).... but it got reversed (commit d851ffb, April 2d 2014)!
It might come soon, but not in its current implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...