如何在 git repo 中添加缺少的 origin/HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17639383/
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 08:46:39  来源:igfitidea点击:

How to add missing origin/HEAD in git repo

git

提问by Dirk

I have a working git repo. However, I miss the remotes/origin/HEAD -> origin/masterwhen typing git branch -a. Why is the HEAD missing and how can I add the missing HEAD to my repo?

我有一个可用的 git 仓库。但是,我想念remotes/origin/HEAD -> origin/master打字时的git branch -a. 为什么缺少 HEAD,如何将缺少的 HEAD 添加到我的存储库中?

回答by Nevik Rehnel

Original Answer:

原答案:

The origin's HEADis only fetched when you clone the repo. If you otherwise add the remote (e.g. by using git remote addor by renaming another existing remote), this ref will not exist, because there is not reason to have it.

起源的HEAD,当你克隆回购只是牵强。如果您以其他方式添加遥控器(例如,通过使用git remote add或重命名另一个现有遥控器),则此 ref 将不存在,因为没有理由拥有它。

Remote repos should be bare reposin most cases, and in bare repos HEADmerely points to the "default" branch. This is only relevant at one time: when cloning. Therefore, after cloning, any remote HEADs are no longer important to your clone and Git will not fetch that ref from any remote.

大多数情况下,远程仓库应该是裸仓库,而在裸仓库中HEAD仅指向“默认”分支。这仅在一次相关:克隆时。因此,克隆后,任何 remoteHEAD对您的克隆不再重要,Git 将不会从任何远程获取该 ref。



As user lesmanarequested, I looked into this again to find some more information:

正如用户lesmana 所要求的,我再次查看了这个以找到更多信息:

"How to remove origin/HEAD?"

“怎么去除origin/HEAD?”

You do not have to do this.

您不必这样做。

The ref has no influence on the way your repo operates, and a ref is literally just a small text file on your file system, so it takes up nearly no space (just a few bytes).

ref 对你的 repo 的运行方式没有影响,而且 ref 只是你文件系统上的一个小文本文件,所以它几乎不占用空间(只有几个字节)。

If you still want to remove it, you can use

如果你仍然想删除它,你可以使用

git update-ref -d refs/remotes/origin/HEAD

(if you want to remove a remote HEAD that is not on origin, use the respective remote's name instead).

(如果要删除不在 上的远程 HEAD origin,请改用相应的远程名称)。

"How to create origin/HEAD?"

“怎么创造origin/HEAD?”

As pointed out earlier, a remote HEAD ref is only used for cloning, and never required by Git at a later time. Unless you manually use it (which does not seem very useful, you can just use the branch it points to), there is no reason to manually create it.

如前所述,远程 HEAD ref 仅用于克隆,以后 Git 不再需要。除非你手动使用它(这看起来不是很有用,你可以直接使用它指向的分支),否则没有理由手动创建它。

Nevertheless, if you absolutely must, you can use

然而,如果你绝对必须,你可以使用

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master

"Why is a remote HEAD not removed automatically by clone if it really is useless?"

“如果远程 HEAD 真的没用,为什么它不会被克隆自动删除?”

There is no specific reason, from what I can tell, other than maybe inform the user explicitly of which branch is considered the default branch in the cloned remote repo. However, as I mentioned above, its existence causes no problems and uses up almost no space, so there is no real reason to remove it.

据我所知,除了可能明确通知用户哪个分支被视为克隆远程存储库中的默认分支之外,没有具体的原因。但是,正如我上面提到的,它的存在不会造成任何问题并且几乎不占用空间,因此没有真正的理由将其删除。

For more details, feel free to ask the Git devs directly at the Git mailing list :)

有关更多详细信息,请随时直接在 Git 邮件列表中询问 Git 开发人员:)

"What is the exact reason why clone needs it?"

“克隆人需要它的确切原因是什么?”

None of the man pages explain directly that git clonealways uses this, but it finds sidelong mentions in some places.

没有一个手册页直接解释git clone总是使用这个,但它在某些地方找到了侧面提及。

For example, man git clonesays:

例如man git clone说:

--branch <name>
-b <name>

Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository's HEAD, point to <name>branch instead. [...]

--branch <name>
-b <name>

不要将新创建的 HEAD 指向克隆存储库的 HEAD 指向的<name>分支,而是指向分支。[...]

and

--[no-]single-branch

Clone only the history leading to the tip of a single branch, either specified by the --branchoption or the primary branch remote's HEAD points at. [...]

--[no-]single-branch

仅克隆导致单个分支尖端的历史记录,由--branch选项指定或主分支远程的 HEAD 指向。[...]

Furthermore, man gitrepository-layoutsays:

此外,man gitrepository-layout说:

HEAD

[...] It does not mean much if the repository is not associated with any working tree (i.e. a barerepository), but a valid Git repository musthave the HEADfile; some porcelains may use it to guess the designated "default" branch of the repository (usually master).

HEAD

[...] 如果存储库不与任何工作树(即存储库)相关联,则意义不大,但有效的 Git 存储库必须具有该HEAD文件;一些瓷器可能会使用它来猜测存储库的指定“默认”分支(通常是master)。

This means that

这意味着

  • a) the remote repoitself must have the HEAD ref (to be valid), even though it is not semantically important (other than to point out the default branch)
  • b) git cloneuses the remote repo's HEAD ref to determine where to point the local HEAD(unless you specify an override with --branch). To work with that ref, it must locally create it (therefore origin/HEAD). As mentioned above, it is then simply not deleted.
  • a) 远程仓库本身必须有 HEAD ref(有效),即使它在语义上并不重要(除了指出默认分支)
  • b)git clone使用远程 repo 的 HEAD ref 来确定指向本地的位置HEAD(除非您使用 指定覆盖--branch)。要使用该 ref,它必须在本地创建它(因此origin/HEAD)。如上所述,它然后不会被删除。