How can I specify a branch/tag when adding a Git submodule?
Note: Git 1.8.2 added the possibility to track branches. See some of the answers below.
It's a little confusing to get used to this, but submodules are not on a branch. They are, like you say, just a pointer to a particular commit of the submodule's repository.
This means, when someone else checks out your repository, or pulls your code, and does git submodule update, the submodule is checked out to that particular commit.
This is great for a submodule that does not change often, because then everyone on the project can have the submodule at the same commit.
If you want to move the submodule to a particular tag:
cd submodule_directory
git checkout v1.0
cd ..
git add submodule_directory
git commit -m "moved submodule to v1.0"
git push
Then, another developer who wants to have submodule_directory changed to that tag, does this
git pull
git submodule update --init
git pull
changes which commit their submodule directory points to. git submodule update
actually merges in the new code.
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:[submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git
-
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:[submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git branch = master
Note: only branch name is supported in a
.gitmodules
file, but SHA and TAG are not supported! (instead of that, the branch's commit of each module can be tracked and updated using "git add .
", for example likegit add ./SubmoduleTestRepo
, and you do not need to change the.gitmodules
file each time)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:git submodule update --remote
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.