How do I git clone --recursive and checkout master on all submodules in a single line?

后端 未结 5 1039
失恋的感觉
失恋的感觉 2020-12-04 10:45

I really like this command to fetch a repo with submodules:

git clone git@github.com:my_user/my_repo.git --recursive

However, the submodule

相关标签:
5条回答
  • 2020-12-04 11:21

    The question is why you checkout master. Your sub modules are pinned to a specific sha - that's also why the sub module clones are fixed to that specific commit. By not pointing to a specific sha an external repo could easily break your builds. Most definitely not what you want. Better update consciously. Builds should be reproducible and as fix as possible.

    0 讨论(0)
  • 2020-12-04 11:36

    Perhaps you should consider gitslave as an alternative to git-submodule, depending on your development workflow it may be better (or worse). Specifically to your issue, gitslave keeps all members of the superproject on the same branch (absent specific git (not gitslave) commands to do otherwise, and even then many commands warn you when you are on different branches).

    Gitslave is useful when you control and develop on the subprojects at more of less the same time as the superproject, and furthermore when you typically want to tag, branch, push, pull, etc all repositories at the same time.

    git-submodule is better when you do not control the subprojects or more specifically wish to fix the subproject at a specific revision even as the subproject changes.

    0 讨论(0)
  • 2020-12-04 11:37

    After cloning the repository containing your submodules, the following command will checkout the master branch on all of these in one go:

    git submodule foreach --recursive git checkout master
    
    0 讨论(0)
  • 2020-12-04 11:38

    How about:

    git submodule update --init --recursive
    

    To initialize all submodules and submodules inside submodules. Not sure if this will checkout master though.

    0 讨论(0)
  • 2020-12-04 11:39

    As already answered

    git submodule foreach --recursive git checkout master
    

    does the job for the branch master.

    But if it is a branch that is not present in all of the submodules one can use

    git submodule foreach --recursive "git checkout branchname || true"
    

    Otherwise the command will fail on the first repo not having the specified branch.

    0 讨论(0)
提交回复
热议问题