git 如何使带有分离 HEAD 的子模块连接到实际 HEAD?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8642668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:18:06  来源:igfitidea点击:

How to make submodule with detached HEAD to be attached to actual HEAD?

gitgit-submodules

提问by Eonil

When I add a Git submodule to a Git repository like this,

当我将 Git 子模块添加到这样的 Git 存储库时,

git submodule add ssh://server/proj1/ proj1
git submodule init
git submodule update

the added submodule will be in detached HEADmode. I don't know well what that is, but I know that the submodule will be linked to specific revision of the target repository.

添加的子模块将处于分离的 HEAD模式。我不太清楚那是什么,但我知道子模块将链接到目标存储库的特定修订版。

I don't know how it actually works, anyway it looks like a proxy branch exists there. I solved this by switching to master branch.

我不知道它实际上是如何工作的,无论如何看起来那里存在一个代理分支。我通过切换到主分支解决了这个问题。

cd proj1
git checkout master

This will switch current branch actual master HEAD, but this does not update the linkage. So If you clone the whole repository again, it will still be linked to old revision.

这将切换当前分支实际主 HEAD,但这不会更新链接。因此,如果您再次克隆整个存储库,它仍将链接到旧版本。

If I want to make it to be linked to most recent revision (HEAD) always, what should I do?

如果我想让它总是链接到最近的修订版 (HEAD),我该怎么办?

回答by VonC

Update March 2013

2013 年 3 月更新

Git 1.8.2added the possibility to track branches.

Git 1.8.2添加了跟踪分支的可能性。

"git submodule" started learning a new mode to integrate with the tip of the remote branch(as opposed to integrating with the commit recorded in the superproject's gitlink).

" git submodule" 开始学习一种新模式来与远程分支的提示集成(而不是与记录在超级项目的 gitlink 中的提交集成)。

# add submodule to track master branch
git submodule add -b master [URL to Git repo];

# update your submodule
git submodule update --remote 

See also the Vogella's tutorial on submodules.

另请参阅Vogella 的子模块教程



Original answer (December 2011)

原始答案(2011 年 12 月)

added submodule will be in detached HEAD mode

添加的子模块将处于分离的 HEAD 模式

Yes, a submodule is about referencing a specific commit, and not a branch.
So:

是的,子模块是关于引用特定的提交,而不是分支。
所以:

  • If you checkout a commit SHA1 (or a tag), you are in a detached HEAD mode.
  • If you checkout a branch (like you did with masterbranch of the submodule), you can create other commits on top of that branch (but you will have to go back to the parent repo in order to commit said parent as well, for you need to record the new submodule commit you created)
  • 如果您检出提交 SHA1(或标签),则您处于分离的 HEAD 模式。
  • 如果您签出一个分支(就像您master对子模块的分支所做的那样),您可以在该分支之上创建其他提交(但您必须返回到父存储库才能提交所述父存储库,因为您需要记录您创建的新子模块提交)

See "True nature of submodules" for more.

有关更多信息,请参阅“子模块的真实性质”。

If you always wanted the latest commit of another repo, the simplest way would be to merge them together (for instance with subtree merging).
See "Merge 2 same repository GIT" for the details and references.

如果你总是想要另一个 repo 的最新提交,最简单的方法是将它们合并在一起(例如使用子树合并)。
有关详细信息和参考,请参阅“合并 2 个相同的存储库 GIT”。