How does git submodule add -b
work?
After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init
) will be at a specific commit, not the branch itself (git status
on the submodule shows "Not currently on any branch").
I can't find any information on .gitmodules
or .git/config
about the submodule's branch or any specific commit, so how does Git figure it out?
Also, is it possible to specify a tag instead of a branch?
I'm using version 1.6.5.2.
Git 1.8.2 added the possibility to track branches.
See also Git submodules
Git submodules are a little bit strange - they're always in "detached head" mode - they don't update to the latest commit on a branch like you might expect.
This does make some sense when you think about it, though. Let's say I create repository foo with submodule bar. I push my changes and tell you to check out commit a7402be from repository foo.
Then imagine that someone commits a change to repository bar before you can make your clone.
When you check out commit a7402be from repository foo, you expect to get the same code I pushed. That's why submodules don't update until you tell them to explicitly and then make a new commit.
Personally I think submodules are the most confusing part of Git. There are lots of places that can explain submodules better than I can. I recommend Pro Git by Scott Chacon.
To switch branch for a submodule (assuming you already have the submodule as part of the repository):
cd
to root of your repository containing the submodules.gitmodules
for editingpath = ...
andurl = ...
that saysbranch = your-branch
, for each submodule; save file.gitmodules
.$ git submodule update --remote
...this should pull in the latest commits on the specified branch, for each submodule thus modified.
I have this in my .gitconfig file. It is still a draft, but proved useful as of now. It helps me to always reattach the submodules to their branch.
I'd like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.
You know you have a Git submodule when you have these two things.
Your
.gitmodules
has an entry like so:You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as "submodule" objects. Or do
git submodule status
from a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.Whenever you do a
git submodule update
, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the.gitmodules
.Now, all the
-b
does is add one line in your.gitmodules
file. So following the same example it would look like this:EDIT: only branch name is supported above, not SHA or TAG.
The submodule object is still pointing at a specific commit. The only thing that the
-b
option buys you is the ability to add a--remote
flag to your update as per Vogella's answer:Instead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.
git submodule add -b
is not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the.gitmodules
file and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.