git中的HEAD和tip是什么关系?

What is the relationship between HEAD and tip in git?

在 Arch Linux 打包中,我遇到了术语 git 提示 (What is a branch tip in Git?)。作为 git 用户,我习惯于将我认为相同或至少相似的概念称为 HEAD。

两者有什么关系?为什么同一事物有两个名称(假设它们是),是否存在一个名称更合适的情况?

HEAD 是 git 中的一个特殊术语。它指的是最近的提交此刻在你现在坐的图表中的位置

如果你在一个分支中,并且在它的尖端,那么分支尖端也被称为 HEAD

如果您签出另一个分支,则该分支的提示将变为 HEAD

如果您签出一个根本不是分支提示的随机提交,例如

git checkout HEAD~3

(这很可能会让你进入 detached-head 状态),那么该提交被称为 HEAD,尽管它不一定与任何分支的提示一致。

这是一个与 Git 模型本身密切相关的问题:所有提交(初始提交除外)至少有一个父级:

               +--- G
               +
A +- B +- C +- D +- H
     +    +
     +----|--- E
          |
          +--- F

这意味着所有的提交都可以被认为是有向无环图的节点,其中初始提交是根;此有向无环图的叶子是分支提示。

回复 HEAD,来自 git(1):

Named pointers called refs mark interesting points in history. A ref may contain the SHA-1 name of an object or the name of another ref. Refs with names beginning ref/head/ contain the SHA-1 name of the most recent commit (or "head") of a branch under development. SHA-1 names of tags of interest are stored under ref/tags/. A special ref named HEAD contains the name of the currently checked-out branch.

此描述不适用于分离的 HEAD 场景(因为分支实际上没有名称),但如果您将这种情况视为匿名分支,它仍然描述得很好。

简而言之,HEAD 是一个指向提交的指针(从某种意义上说——将 HEAD 视为指向 ref 的指针通常更准确,ref 又指向提交),而分支提示指的是特定类型的提交,即没有后代的提交。