8

For a normal git repo you can do:

git clone --branch 4.1.1 https://github.com/WordPress/WordPress.git . --depth 1

Which will give a WP repo at tag 4.1.1

For a submodule I did get the below but I can't figure out how to do it for just one tag.

git submodule add --depth 1  https://github.com/WordPress/WordPress.git wp

How do I checkout a submodule to 1 tag at 1 depth?

I don't mind doing a few more commands afterwards but if possible in one command even better.

TLDR: I want a submodule at a tag. Which .git(/module) folder is as small as possible.

janw
  • 6,672
  • 6
  • 26
  • 45
  • I suspect with http://stackoverflow.com/a/17693008/6309, but I am still looking for the right command. – VonC Mar 23 '15 at 13:49
  • This is a great question and exactly what I have been trying to do myself. It was frustrating having such a huge .git dir just to track one branch. Thanks OP and @VonC for your answer. Probably a question in itself, but if you then want to checkout a new branch, will this clean up the old tag? – Alexander Holsgrove Oct 17 '16 at 10:00
  • @AlexHolsgrove if the tag is already imported, it will still be there, referencing its own commit: creating a new branch won't change that. – VonC Oct 17 '16 at 11:14
  • No tag as yet as I've not done an init on the submodule. I presume you have to git add the submodule first before doing the fetch & checkout as you explain in your answer? – Alexander Holsgrove Oct 17 '16 at 14:17

1 Answers1

2

Considering a submodule might not comes with its tags, you might be tempted to do first (as suggested in "Can git submodule update be made to fetch tags in submodules?"):

git submodule foreach --recursive 'git fetch --tags'

But as I explained in "Does “git fetch --tags” include “git fetch”?", that would not fetch just the tags, but also all the associated commits (since git 1.9.0, February 2014)

Instead, you can go in the submodule, and fetch that tag content with a depth of 1:

git fetch --depth=1 origin refs/tags/<tag>:refs/tags/<tag>
git checkout <tag>
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This doesn't work the `git fetch --tags` will fetch everything and then the .git folder is still bigger then 100 mb. Even after the specific checkout it's still big – janw Mar 27 '15 at 14:03
  • @janw yes, I just saw my old answer http://stackoverflow.com/a/20608181/6309! Can you try and test if you can fetch one tag at depth 1? `git fetch --depth=1 origin 'refs/tags/:refs/tags/'`? – VonC Mar 27 '15 at 14:10
  • YES yes that's it. If you could update your answer to include that I'll award the bounty – janw Mar 27 '15 at 14:20
  • `You may award your bounty in 19 hours.` I'll probably forget it on saterday. Got a reminder set for monday – janw Mar 27 '15 at 14:27
  • @janw darn it: tomorrow would have been nice: it is usually very hard to come up with votes on a saturday ;) – VonC Mar 27 '15 at 14:27
  • I'll try, no promises (A) – janw Mar 27 '15 at 14:30