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

后端 未结 4 937
慢半拍i
慢半拍i 2021-02-04 02:57

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

4条回答
  •  囚心锁ツ
    2021-02-04 03:31

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

提交回复
热议问题