2023-04-02 05:00:03

Git中的HEAD是什么?

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

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

但Git HEAD到底是什么?


当前回答

引用别人的话:

A head is simply a reference to a commit object. Each head has a name (branch name or tag name, etc). By default, there is a head in every repository called master. A repository can contain any number of heads. At any given time, one head is selected as the “current head.” This head is aliased to HEAD, always in capitals". Note this difference: a “head” (lowercase) refers to any one of the named heads in the repository; “HEAD” (uppercase) refers exclusively to the currently active head. This distinction is used frequently in Git documentation.

可以在这里找到另一个很好的源代码,它可以快速覆盖git的内部工作原理(因此可以更好地理解heads/HEAD)。引用(ref:)或头或分支可以被看作是粘贴在提交历史记录中的提交上的便利贴。通常它们指向一系列提交的提示,但它们可以随着git checkout或git reset等移动。

其他回答

我推荐github开发者Scott Chacon的定义:

Head是你当前的分支。这是一个象征性的参照。它是对分支的引用。你总是有HEAD,但HEAD会指向其他指针之一,指向你所在的某个分支。它是您下次提交的父节点。它应该是最后签入工作目录的内容。这是您的工作目录的最后一个已知状态。

整个视频将公平地介绍整个git系统,所以如果有时间,我也建议你看一遍。

看一下如何创建和使用分支

HEAD实际上是一个文件,其内容决定了HEAD变量的引用位置:

$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/refs/heads/master
35ede5c916f88d8ba5a9dd6afd69fcaf773f70ed

在这个存储库中,HEAD文件的内容引用了名为refs/heads/master的第二个文件。文件refs/heads/master包含主分支上最近提交的哈希值。

结果是HEAD指向主分支提交从.git/refs/heads/master文件。

Head指向当前签出分支的顶端。

在您的存储库中,有一个.git文件夹。在这个位置打开文件:.git\refs\heads。该文件(在大多数情况下是master)中的(sha-1哈希)代码将是最近的提交,即在命令git日志的输出中看到的代码。关于.git文件夹的更多信息:http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html

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

图3 - 5。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.