您可以看到Git文档中这样说
分支必须在HEAD中完全合并。
但Git HEAD到底是什么?
您可以看到Git文档中这样说
分支必须在HEAD中完全合并。
但Git HEAD到底是什么?
当前回答
看一下如何创建和使用分支
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文件。
其他回答
我还在弄清楚git的内部结构,到目前为止,我已经弄清楚了这个:
假设当前的分支是master。
HEAD是你的.git/目录下的一个文件,通常看起来像这样:
% cat .git/HEAD
ref: refs/heads/master
Refs /heads/master本身是一个文件,通常包含master最新提交的哈希值:
% cat .git/refs/heads/master
f342e66eb1158247a98d74152a1b91543ece31b4
如果你做git日志,你会看到这是master的最新提交:
% git log --oneline
f342e66 (HEAD -> master,...) latest commit
fa99692 parent of latest commit
所以我的想法是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只是您签出的最后一次提交的标记。
这可以是一个特定分支的顶端(例如“master”),也可以是某个分支的中间提交(“detached head”)。
看看http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is
图3 - 5。HEAD文件指向你所在的分支。