2

How can I checkout a git repo including its submodules?

e.g. I have a repo A, with a submodule B, the commit history is as follows:

  • f7fe0a4 A, bcbaec2 B (latest)
  • 206c8dd A, bcbaec2 B
  • 0fd667c A, bcbaec2 B
  • 0ec21a1 A, e79fea0 B (older version)

I want to temporarily go back to an older version of A, so I stashed local changes in A and B, then git checkout 0ec21a1

But Im still on the latest version of the submodule. I expected the submodule to updated according to the parent version. My understanding from this question, submodule tag is that git does not manage submodules when you checkout the parent, is this correct?

My real commit history is more complicated and there are several submodules. Is there a way to determine what version of submodule B was used in commit xxx of A? I can get approximate versions by comparing the git logs but it's rather painful...

Damo
  • 374
  • 5
  • 11
  • Possible duplicate of [Why is git submodule update not automatic on git checkout?](https://stackoverflow.com/questions/1899792/why-is-git-submodule-update-not-automatic-on-git-checkout) – phd Aug 07 '18 at 13:06
  • Yes, that response answers this question. So what I was missing was 'git submodule update' – Damo Aug 07 '18 at 16:13

1 Answers1

3

git checkout 0ec21a1 --recurse-submodules will update the content of submodule B (if it's already initialized) according to the commit which is stored in parent project A (in this case will checkout e79fea0)

nicole
  • 91
  • 10
  • Thanks, that should work, unfortunately Im stuck with v2.8 of git this feature was added in v2.13. In any case adding git submodule update after the checkout works – Damo Aug 07 '18 at 16:14