2023-04-02 05:00:03

Git中的HEAD是什么?

您可以看到Git文档中这样说

分支必须在HEAD中完全合并。

但Git HEAD到底是什么?


当前回答

Git都是关于提交的。 Head指向当前签出的提交。

$ git cat-file -t HEAD
commit

无论何时签出一个分支,HEAD都指向该分支上的最新提交。HEAD的内容可检查如下(主分支):

$ cat .git/refs/heads/master
  b089141cc8a7d89d606b2f7c15bfdc48640a8e25

其他回答

HEAD几乎字面上就是分支的头。所以当你观察一个分支时,你看到的是最新的提交,也就是这个分支的头。但是,您可以将自己指向该分支历史上更远的另一个提交,当您这样做时,您是在将HEAD移动到前一个提交。由于HEAD自然属于分支中的最新提交,因此它被认为是分离的。

视觉表现。每一根树枝都是一条毛毛虫,每一根树枝都是这个生物的一个部分。因此HEAD将位于最前面的段中。如果您将HEAD从该部分移除到另一个要使用的节段,则您已经将HEAD从自然节段中分离出来。希望你能明白。

现在如果你在主分支中分离HEAD,然后签出newFeature,然后再次签出main, HEAD仍然会被分离,并且是在另一个提交之上。我把HEAD看作一面镜子,你可以把它指向你想要的地方。

HEAD只是一个特殊的指针,它指向您当前所在的本地分支。

从Pro Git书籍3.1章Git分支-果壳中的分支,在部分创建一个新的分支:

What happens if you create a new branch? Well, doing so creates a new pointer for you to move around. Let’s say you create a new branch called testing. You do this with the git branch command: $ git branch testing This creates a new pointer at the same commit you’re currently on How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. Note that this is a lot different than the concept of HEAD in other VCSs you may be used to, such as Subversion or CVS. In Git, this is a pointer to the local branch you’re currently on. In this case, you’re still on master. The git branch command only created a new branch — it didn’t switch to that branch.

看看http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is

图3 - 5。HEAD文件指向你所在的分支。

作为一个概念,头是分支中的最新修订。如果每个命名分支有多个头,那么在进行本地提交时可能会创建它,而没有合并,这实际上创建了一个未命名的分支。

为了拥有一个“干净的”存储库,每个命名分支都应该有一个头,并且在本地工作后总是合并到一个命名分支。

Mercurial也是如此。

我认为'HEAD'是当前的检出提交。换句话说,'HEAD'指向当前签出的提交。

如果你刚刚克隆了,没有签出,我不知道它指向什么,可能是一些无效的位置。