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

标签 git

我有一个可用的 git 存储库。但是,在键入 git branch -a 时,我错过了 remotes/origin/HEAD -> origin/master。为什么 HEAD 丢失了?如何将丢失的 HEAD 添加到我的存储库中?

最佳答案

原答案:

来源的HEAD仅在您克隆存储库时获取。如果您以其他方式添加 Remote (例如,通过使用 git remote add 或通过重命名另一个现有 Remote ),此引用将不存在,因为没有理由拥有它。

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


应用户 lesmana 的要求,我再次调查了这个以找到更多信息:

“如何删除 origin/HEAD ?”

您不必这样做。

ref 对您的 repo 操作方式没有影响,ref 实际上只是文件系统上的一个小文本文件,因此它几乎不占用空间(仅几个字节)。

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

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

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

“如何创建origin/HEAD?”

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

不过,如果你绝对需要,你可以使用

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

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

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

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

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

没有任何手册页直接解释 git clone总是使用这个,但它在某些地方发现了旁白。

例如, 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. [...]

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

此外, man gitrepository-layout 说:

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

这意味着

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

关于git - 如何在 git repo 中添加缺失的 origin/HEAD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17639383/

相关文章:

android - 从 AOSP 编译电子邮件应用程序

python - pip 安装本地 git 存储库

git - 有没有办法在 git commit 上重用之前的评论?

git - 使用 prezto 的 zsh 具有与左侧相同级别的右侧提示

git - 拼写检查错误源树

当我知道主分支和 tfs/default 发生更改时,git tfs checkin 工具显示 'nothing to commit'

git - bundler : `bundle package` 带有 :git 源

git - 使用 Windows 身份验证进行私有(private) BitBucket git 存储库?

php - 从 PHP exec() 函数读取 git push 的输出

git - git 如何将当前分支推送到另一个远程/源?