Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git submodule update be made to fetch tags in submodules?

Tags:

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 get new tags in the submodule by cd-ing into the submodule and doing a git fetch --tags there, but I'd really like to do all of this from the outside as it's scripted.

I can't find anything in the git documentation suggesting a way to get git submodule update to include tags (my git version is 1.7.3.5).

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.

Is there a way of getting git submodule update to include tags?

like image 411
Duncan Parkes Avatar asked Sep 21 '11 13:09

Duncan Parkes


People also ask

Does git fetch update submodules?

Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .

What does git submodule update do?

A git submodule update will bring the latest commits into your local Git worktree. In this git submodule update example, we'll show you how branches can seem out of sync between your submodule and the latest commit, and how to issue the appropriate git command to update those git submodules with the latest code.

Can you make changes to a git submodule?

The submodule is just a separate repository. If you want to make changes to it, you should make the changes in its repository and push them like in a regular Git repository (just execute the git commands in the submodule's directory).

Why you should not use git submodules?

This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.


2 Answers

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.

like image 130
jsdalton Avatar answered Jan 27 '23 10:01

jsdalton


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...

like image 28
Mark Longair Avatar answered Jan 27 '23 09:01

Mark Longair