如何在git repo中添加缺少的origin / HEAD [英] How to add missing origin/HEAD in git repo

查看:439
本文介绍了如何在git repo中添加缺少的origin / HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可用的git回购。但是,我错过了遥控器/原点/头部 - >在输入 git branch -a 时输入origin / master 。为什么HEAD缺失,我该如何将缺少的HEAD添加到我的回购中?

解决方案

原始答案:



原始的 HEAD 仅在您克隆回购时获取。如果你以其他方式添加远程(例如通过使用 git remote add 或通过重命名另一个现有远程),这个ref将不存在,因为没有理由它。



远程回购应该是裸回购 a>在大多数情况下,在裸回购 HEAD 中仅指向默认分支。这只是一次相关:克隆时。因此,在克隆之后,任何远程 HEAD 对您的克隆而言都不再重要,并且Git不会从任何远程获取该ref。






当用户 lesmana 请求时,我再次查看此信息以查找更多信息:



如何删除 origin / HEAD



您不必做这个。



这个ref对你的repo操作的方式没有影响,ref只是文件系统上的一个小文本文件,所以它几乎没有空间(只有几个字节)。



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

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

(如果您想要移除远程HEAD不在 origin 上,改为使用相应的远程名称)。



如何创建 origin / HEAD



正如前面指出的,远程HEAD ref仅用于克隆,Git从不需要晚些时候。除非你手动使用它(这似乎不是很有用,你可以使用它指向的分支),没有理由手动创建它。



尽管如此,如果你绝对必须,你可以使用

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



为什么远程HEAD无法通过克隆自动删除,如果它真的没用?



根据我的说法,没有具体的原因,除了可能明确告知用户哪个分支被视为克隆远程仓库中的默认分支。但是,正如我上面提到的,它的存在不会造成任何问题,几乎没有空间,所以没有真正的理由去除它。



更多细节,请随意直接在Git邮件列表中询问Git开发人员:)



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



没有一个手册页直接解释了 git clone 总是使用这个,但是它在一些地方发现了sidelong的提及。



例如, man git clone 说:


- 分支<名称>

-b >



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


- [no-] single-branch



只克隆通往单个分支尖端的历史记录,由 - 分支选项或主分支远程的HEAD指向处指定。 [b]

此外, man gitrepository-layout 表示:


HEAD



[...]如果存储库没有与任何工作树关联(即裸露的存储库),但有效的Git仓库必须具有 HEAD 文件;一些瓷器可能会用它来猜测储存库的指定默认分支(通常是 master )。

这意味着


  • a)远程 repo 本身必须有HEAD ref(有效),即使它在语义上并不重要(除了指出默认分支) git clone 使用远程仓库的HEAD ref决定在哪里指向本地 HEAD (除非用 - branch >指定覆盖)。要使用该参考,它必须在本地创建它(因此 origin / HEAD )。如上所述,它不会被删除。


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

解决方案

Original Answer:

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

Remote repos should be bare repos in most cases, and in bare repos HEAD merely 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.


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

"How to remove 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).

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).

"How to create 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.

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?"

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 :)

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

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

For example, man git clone says:

--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. [...]

and

--[no-]single-branch

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

Furthermore, man gitrepository-layout says:

HEAD

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

This means that

  • a) the remote repo itself 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 clone uses 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.

这篇关于如何在git repo中添加缺少的origin / HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆