git: 子模块跟踪当前分支

git: submodule tracking current branch

根据 git documentation,我应该可以 运行 这个命令:

git submodule add -b . https://my/repo

并添加了子模块,跟踪超级项目当前分支的head。

Branch of repository to add as submodule. The name of the branch is recorded as submodule..branch in .gitmodules for update --remote. A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.

但是当我这样做时,我得到了这个错误:

fatal: 'origin/.' is not a commit and a branch '.' cannot be created from it

我 运行宁 git 2.21。我是否完全错误地阅读了说明?

选项-b 需要存储库https://my/repo 中分支的名称。例如,master:

git submodule add -b master https://my/repo

Git 不能 "track head of the current branch of the super project"。必须明确给出分支。

当您使用 git checkout 切换超级项目的分支时 git 不会自动切换子模块的分支 — 您必须 运行 git submodule update 手动或从 post-checkout勾.

Why is git submodule update not automatic on git checkout?

https://whosebug.com/search?q=%5Bgit-submodules%5D+switch+branch

git submodule add 似乎没有任何代码可以对点进行任何类型的检查。克隆后,它会尝试检查分支“.”,这自然不存在。

但是,git submodule update --remote 有检查,并使用“.”特别地

要使其正常工作,您需要执行以下操作:

  1. git submodule add -b master https://my/repo
  2. git submodule set-branch --branch . REMOTE_NAME
  3. git submodule update --remote
  4. 利润

每次您 运行 超级项目的更新都会得到分支的提示。

我不确定是文档不清楚,还是在添加子模块时存在错误。

编辑:更新了 Akom 的评论,因为它比编辑 .gitmodules 文件手动添加点作为分支名称更容易。