How can I specify a branch/tag when adding a Git submodule?

前端 未结 12 1241
旧巷少年郎
旧巷少年郎 2020-11-21 06:39

How does git submodule add -b work?

After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init

12条回答
  •  时光说笑
    2020-11-21 07:04

    The only effect of choosing a branch for a submodule is that, whenever you pass the --remote option in the git submodule update command line, Git will check out in detached HEAD mode (if the default --checkout behavior is selected) the latest commit of that selected remote branch.

    You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules. The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote. If you pass also the --depth parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update command line!! -- , it will implicitly behave like explained in the git-clone(1) documentation for git clone --single-branch when the explicit --branch parameter is missing, and therefore it will clone the primary branch only.

    With no surprise, after the clone stage performed by the git submodule update command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with

    fatal: Needed a single revision

    Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'

提交回复
热议问题