问题描述
即使HEAD对于Git分支的名称来说绝对是一个糟糕的选择,它仍然是一个有效的分支名称。如果你碰巧有一个名为HEAD的分支,有没有办法明确地引用实际的 HEAD
符号引用? 该分支可以被引用为 refs / heads / HEAD
,但是 HEAD
本身怎么样?
使用 HEAD
导致 refname'HEAD'含糊不清
在任何以< commit>
作为参数传递的地方发生错误。
根据,如果 HEAD
和 refs / heads / HEAD
存在,所选修订为 HEAD
(即不是名为 HEAD
的分支)。
这实际上是大多数的情况下,但 git checkout
更喜欢修订的分支名称,所以 git checkout HEAD
解决分支,而不是当前提交。
还有其他命令也可以选择分支名称,例如 git branch -f HEAD newrev
或 git分支-D HEAD
引用分支,但在这里没有真正的模糊空间: git branch
是明显地在分支上工作。
其他处理程序通常会将分支名称或修订说明符传递给 git rev -parse
或 git rev-list
,这些行为与。
请注意,类似的情况可能会发生在更现实的分支名。就在昨天,我创建了一个处理某些以太网项目的分支,并将分支命名为 e1000
...,它看起来像一个缩写的SHA-1。名为 facade的分支
遭受同样的命运。
Even though "HEAD" is definitely a poor choice for the name of a Git branch, it is still a valid branch name. If you happen to have branch named "HEAD", is there a way how to unambiguously refer to the actual HEAD
symbolic reference?
The branch can be referenced as refs/heads/HEAD
, but what about the HEAD
itself?
Using just HEAD
results in an refname 'HEAD' is ambiguous
error anywhere where a <commit>
is passed as an argument.
According to gitrevisions, if both HEAD
and refs/heads/HEAD
exist, the selected revision is HEAD
(i.e., not the branch named HEAD
).
This is in fact the correct answer for most situations, but git checkout
prefers the branch-name to the revision, so git checkout HEAD
resolves to the branch, rather than the current commit.
There are other commands that also choose the branch name, e.g., git branch -f HEAD newrev
or git branch -D HEAD
refers to the branch, but here there's no real room for ambiguity: git branch
is obviously going to work on the branch.
Other handlers generally pass a branch name or a revision specifier to git rev-parse
or git rev-list
, and those behave as documented in gitrevisions.
Note that similar cases can occur with more realistic branch names. Just yesterday I created a branch for dealing with a certain ethernet items, and named the branch e1000
... which looks like an abbreviated SHA-1. A branch named facade
suffers the same fate.
这篇关于GIT:当有一个叫HEAD的分支时,如何明确地引用当前的HEAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!