I have a git repository which uses a submodule which I\'d like to point at an annotated tag, but when I do git submodule update
new tags don\'t get fetched. I can g
Late answer here, but I'm surprised that no one mentioned git submodule foreach
. This is basically the way I solved the exact problem you encountered:
git submodule foreach --recursive 'git fetch --tags'
git submodule update --recursive
--recursive
flag is there to recurse into child submodules.
you can script it so that you do a (cd path-to-submod && git fetch)
wrapping the commands in brackets puts the environment in a subshell, meaning, you don't have to CD back out to where you were.
Hope this helps
git submodule update
doesn't fetch anything if your submodules are up-to-date. So this might be a misapprehension.
git submodule update
will bring the working directory contents of the submodules in your repository to the defined state (i.e. check out the submodule commit that has been defined in your repository).
A better way would be to cd
into your submodule (which is a Git repository itself) and simply run git fetch --tags
and then git checkout some-tag
. After that the submodule has been updated in your working directory to some-tag
and you can stage it for committing.
git submodule
is implemented as a shell script, so it's easy to see what it's doing — it might be at /usr/lib/git-core/git-submodule
if you're using a packaged version. Essentially it just runs git-fetch
in the submodule if it the object name (SHA1sum) stored in the main project's tree doesn't match the version checked out in the submodule, as Koraktor points out.
The documentation for git fetch (or man git-fetch
while kernel.org is down) says that it should fetch every tag that points to a downloaded object, and the downloaded objects will include every commit that's an ancestor of every branch that's fetched. That means it's surprising to me that you don't get all the relevant tags on a git submodule update
.
If it's the case that what you really want is for your script is to try to set a new submodule version and commit that result, I don't think that git submodule update
is the tool that you want - that's just for making sure that your submodules are at the right version based on what's currently in the main project's commit. Instead you should just do something like:
( cd my-submodule && \
git fetch && \
git fetch --tags && \
git checkout my-tag )
git add my-submodule
git commit -m 'Update the submodule to the "my-tag" version' my-submodule
(I added an extra git fetch --tags
just in case your tag isn't one that points to a downloaded commit.)
Obviously there is another possibility - to point the submodule at the commit which the tag points to rather than the tag itself, but this doesn't seem as neat.
Well, the only thing that's stored in the main project's tree for the submodule is just the hash of the commit object, so even if there were a command that said "set my submodule to the tag my-tag
in that submodule", it would end up just storing the hash corresponding to that tag anyway...