0

On the question:

  1. How to correctly call a git submodule symlinked?

Was figured it out it is necessary to checkout the git submodules to their default branch when a git clone --recursive is performed, but how to do that?

I tried searching and found this other question:

  1. Easy way pull latest of all submodules

Suggesting to use the command git clone --recurse-submodules but after cloning the repository its submodule still not checkout on their default branches.

user
  • 7,316
  • 9
  • 70
  • 122

2 Answers2

2

git submodule foreach git checkout master

phd
  • 69,888
  • 11
  • 97
  • 133
  • Thanks, It sure helps but what happens with those submodules which its default branch is not master? – user Jun 16 '17 at 17:59
2

You can use git submodule foreach to run an arbitrary command in each submodule. The --recursive flag will recurse through the submodules' submodules. git remote show [name-of-remote] will say which branch [name-of-remote] currently has active. Combining them with a few other tools to clean up git remote show's output gives:

git submodule foreach --recursive "git checkout $(git remote show origin | grep 'HEAD branch' | sed 's/.*: //')"

This is, of course, dependent on already having cloned the submodules.

8bittree
  • 1,709
  • 1
  • 18
  • 24
  • Thanks! I think I can do the command `git clone --recursive https...; git submodule foreach ...`, then it should clone everything and checkout their master. – user Jun 16 '17 at 18:04
  • Is there a solution platform independent or a Windows version of it? If I run this command on Windows, it would not have available the `grep 'HEAD branch' | sed 's/.*: //'` part. – user Jun 16 '17 at 18:10
  • 1
    @user I believe `grep` and `sed` are included in the standard Git for Windows installation, though depending on which options you selected during installation, you may need to use git bash rather than cmd. – 8bittree Jun 16 '17 at 18:13