2023-04-02 05:00:03

Git中的HEAD是什么?

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

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

但Git HEAD到底是什么?


当前回答

HEAD实际上只是一个存储当前分支信息的文件

如果你在git命令中使用HEAD,你就指向了当前的分支

您可以通过查看该文件的数据 猫. /头

其他回答

这两个可能会让你困惑:

head

指向分支最近提交的命名引用。除非使用包引用,否则头文件通常存储在$ GIT_DIR/refs/heads/中。

HEAD

当前分支,或者您的工作树通常是从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.

HEAD实际上只是一个存储当前分支信息的文件

如果你在git命令中使用HEAD,你就指向了当前的分支

您可以通过查看该文件的数据 猫. /头

在阅读了之前所有的答案后,我仍然想要更清楚。git官方网站http://git-scm.com/blog上的这个博客给了我想要的东西:

HEAD:指向最后一次提交快照的指针

Git中的HEAD是指向当前分支引用的指针,而当前分支引用又是指向您所做的最后一次提交或检出到工作目录的最后一次提交的指针。这也意味着它将是你下一次提交的父节点。通常最简单的想法是,HEAD是上次提交的快照。

您可以将HEAD视为“当前分支”。当您使用git签出切换分支时,HEAD修订将更改为指向新分支的尖端。

你可以通过这样做来查看HEAD指向什么:

cat .git/HEAD

在我的例子中,输出是:

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

HEAD可以引用与分支名称不关联的特定修订。这种情况被称为分离HEAD。