您可以看到Git文档中这样说
分支必须在HEAD中完全合并。
但Git HEAD到底是什么?
您可以看到Git文档中这样说
分支必须在HEAD中完全合并。
但Git HEAD到底是什么?
当前回答
HEAD实际上只是一个存储当前分支信息的文件
如果你在git命令中使用HEAD,你就指向了当前的分支
您可以通过查看该文件的数据 猫. /头
其他回答
您可以将HEAD视为“当前分支”。当您使用git签出切换分支时,HEAD修订将更改为指向新分支的尖端。
你可以通过这样做来查看HEAD指向什么:
cat .git/HEAD
在我的例子中,输出是:
$ cat .git/HEAD
ref: refs/heads/master
HEAD可以引用与分支名称不关联的特定修订。这种情况被称为分离HEAD。
假设这不是一种叫做“分离HEAD”的特殊情况,那么,正如O'Reilly Git书第2版第69页所述,HEAD的意思是:
HEAD总是指当前节点上最近的提交 分支。当您更改分支时,HEAD将被更新为引用新的分支 布兰奇的最新提交。
so
HEAD是当前分支的“尖端”。
注意,我们可以使用HEAD表示最近的提交,使用HEAD~表示提示之前的提交,使用HEAD~~或HEAD~2表示更早的提交,以此类推。
一个存储库中可以有多个头。并且头的总数总是等于存储库中存在的分支的总数,这意味着头只是每个分支的最新提交
但是一个存储库只有一个HEAD。HEAD是一个引用,它引用在当前分支完成的最新提交。
它就像git用户的眼睛。无论HEAD引用哪个提交,存储库都开始反映该特定提交期间存储库的条件。
HEAD的基本性质是总是引用当前分支的最新提交,但我们可以通过使用git checkout "commit-hash"将HEAD移动到当前分支的任何提交。
注意:我们可以使用git log——oneline命令轻松获得commit-hash
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.
引用别人的话:
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等移动。