当我克隆我的 git 存储库时,其中一个子模块位于一个名称奇怪的分支中,我认为这意味着它有一个“分离的头”(我什至不确定那是什么意味着)。
如果我检查子模块的主分支,然后运行“git submodule update --init --recursive
”它再次发生。
有人知道这是怎么回事吗?
最佳答案
子模块总是作为分离的 HEAD check out (参见“Why did git detach my head?”),因为父仓库的索引仅包含 SHA1 作为 special entry in its index ,正如 Gary Fixler 所解释的那样的 answer .
即使你configure your submodule to follow a branch (或 convert an existing submodule to follow a branch ),git submodule update --remote
将检查该远程分支的最新 SHA1,但结果默认是分离的 HEAD。< br/>
仅通过向该命令添加 --merge
或 --rebase
(git submodule update --remote (--merge/--rebase)
) 你会得到一个非分离的 HEAD,如 Simba 中所示的 answer ): 默认情况下,master
分支。
如果你没有那样更新(git submodule update --remote (--merge/--rebase)
),那么你需要进入那个子模块并在那里自己创建一个分支.
如果你想为所述子模块做出贡献(在其中进行新的提交),创建一个新分支是个好主意。
cd mySubmodule
git checkout -b aNewBranch
# work
git add .
git commit -m "new commits"
git push -u origin aNewBranch
# record the new submodule state in the parent repo:
cd ..
git add mySubmodule
git commit -m "new state of mySubmodule"
git push
Git 2.16(2018 年第一季度)注意事项:“git checkout --recursive
”可能会覆盖和倒回恰好在子模块中 checkout 的分支的历史记录
存储库,这可能是不可取的。
分离 HEAD
但在这种情况下仍允许递归 check out 成功。
参见 commit 57f22bf (2017 年 7 月 28 日),和 commit 3ef2538 (2017 年 7 月 24 日)作者:Stefan Beller (stefanbeller
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 0b75572 ,2017 年 12 月 6 日)
recursive submodules: detach HEAD from new state
When a submodule is on a branch and in its superproject you run a recursive checkout, the branch of the submodule is updated to what the superproject checks out.
This is very unexpected in the current model of Git as e.g. 'submodule update
' always detaches the submodule HEAD.Despite having plans to have submodule HEADS not detached in the future, the current behavior is really bad as it doesn't match user expectations and it is not checking for loss of commits (only to be recovered via the reflog).
Detach the HEAD unconditionally in the submodule when updating it.
关于克隆和子模块更新后 Git 子模块处于 "detached head"状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794979/