Git 中的 HEAD 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2304087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is HEAD in Git?
提问by bobobobo
You see the Git documentation saying things like
你会看到 Git 文档说这样的话
The branch must be fully merged in HEAD.
分支必须在 HEAD 中完全合并。
But what is Git HEAD
exactly?
但是 GitHEAD
到底是什么?
采纳答案by Greg Hewgill
You can think of the HEAD as the "current branch". When you switch branches with git checkout
, the HEAD revision changes to point to the tip of the new branch.
您可以将 HEAD 视为“当前分支”。当您使用 切换分支时git checkout
,HEAD 修订将更改为指向新分支的尖端。
You can see what HEAD points to by doing:
您可以通过执行以下操作来查看 HEAD 指向的内容:
cat .git/HEAD
In my case, the output is:
在我的情况下,输出是:
$ cat .git/HEAD
ref: refs/heads/master
It is possible for HEAD to refer to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.
HEAD 可以引用与分支名称无关的特定修订。这种情况称为分离的 HEAD。
回答by Silfheed
To quote other people:
引用其他人的话:
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.
head 只是对提交对象的引用。每个头部都有一个名称(分支名称或标签名称等)。默认情况下,每个存储库中都有一个名为 master 的 head。一个存储库可以包含任意数量的头。在任何给定时间,一个头被选为“当前头”。这个头的别名是 HEAD,总是大写”。
请注意这一区别:“头”(小写)指的是存储库中的任何一个命名头;“HEAD”(大写)专指当前活动的头部。这种区别在 Git 文档中经常使用。
Another good source that quickly covers the inner workings of git (and therefor a better understanding of heads/HEAD) can be found here. References (ref:) or heads or branches can be considered like post-it notes stuck onto commits in the commit history. Usually they point to the tip of series of commits, but they can be moved around with git checkout
or git reset
etc.
可以在此处找到另一个快速涵盖 git 内部工作原理(从而更好地理解 head/HEAD)的好资源。引用 (ref:) 或 head 或 branch 可以被视为贴在提交历史中的提交上的便利贴。通常它们指向一系列提交的尖端,但它们可以通过git checkout
或git reset
等移动。
回答by jasoares
I recommend this definition from github developer Scott Chacon [video reference]:
我推荐 github 开发人员 Scott Chacon [视频参考] 的这个定义:
Head is your current branch. It is a symbolic reference. It is a reference to a branch. You always have HEAD, but HEAD will be pointing to one of these other pointers, to one of the branches that you're on. It is the parent of your next commit. It is what should be what was last checked-out into your working directory... This is the last known state of what your working directory was.
Head 是你当前的分支。这是一个象征性的参考。它是对分支的引用。你总是有 HEAD,但 HEAD 将指向这些其他指针之一,指向你所在的分支之一。它是您下一次提交的父级。它应该是上次检出到您的工作目录中的内容......这是您的工作目录的最后一个已知状态。
The whole video will give a fair introduction to the whole git system so I also recommend you to watch it all if have the time to.
整个视频将对整个 git 系统进行公平的介绍,因此我也建议您在有时间的情况下全部观看。
回答by Alexandr
HEAD is just a special pointer that points to the local branch you're currently on.
HEAD 只是一个特殊的指针,指向您当前所在的本地分支。
From the Pro Gitbook, chapter 3.1 Git Branching - Branches in a Nutshell, in the section Creating a New Branch:
来自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.
如果你创建一个新分支会发生什么?好吧,这样做会创建一个新的指针供您四处移动。假设您创建了一个名为 testing 的新分支。您可以使用 git branch 命令执行此操作:
$ git branch testing
这会在您当前所在的同一提交中创建一个新指针
Git 如何知道你当前在哪个分支?它保留了一个称为 HEAD 的特殊指针。请注意,这与您可能习惯的其他 VCS(例如 Subversion 或 CVS)中的 HEAD 概念有很大不同。在 Git 中,这是指向您当前所在的本地分支的指针。在这种情况下,您仍然在 master 上。git branch 命令只创建了一个新分支——它没有切换到那个分支。
回答by nonopolarity
Assuming it is not a special case called "detached HEAD", then, as stated in the O'Reilly Git book, 2nd edtion, p.69, HEAD
means:
假设它不是称为“分离的 HEAD”的特殊情况,那么,正如 O'Reilly Git 书籍,第 2 版,第 69 页中所述,HEAD
意味着:
HEAD
always refers to the most recent commit on the current branch. When you change branches,HEAD
is updated to refer to the new branch's latest commit.
HEAD
总是指当前分支上的最新提交。当您更改分支时,HEAD
更新为引用新分支的最新提交。
so
所以
HEAD
is the "tip" of the current branch.
HEAD
是当前分支的“提示”。
Note that we can use HEAD
to refer to the most recent commit, and use HEAD~
as the commit before the tip, and HEAD~~
or HEAD~2
as the commit even earlier, and so forth.
请注意,我们可以使用HEAD
来引用最近的提交,并HEAD~
用作提示之前的提交,HEAD~~
或者HEAD~2
甚至更早的提交,依此类推。
回答by De Novo
There is a, perhaps subtle, but important misconception in a number these answers. I thought I'd add my answer to clear it up.
在这些答案中存在一个可能微妙但重要的误解。我想我会添加我的答案来清除它。
What is
HEAD
?
什么是
HEAD
?
HEAD is YOU
头是你
HEAD
is a symbolic reference pointing to wherever you are in your commit history. It follows you wherever you go, whatever you do, like a shadow. If you make a commit, HEAD
will move. If you checkout something, HEAD
will move. Whatever you do, if you have moved somewhere new in your commit history, HEAD
has moved along with you. To address one common misconception: you cannot detach yourself from HEAD
. That is not what a detached HEAD state is. If you ever find yourself thinking: "oh no, i'm in detached HEAD state! I've lost my HEAD!" Remember, it's your HEAD. HEAD is you. You haven't detached from the HEAD, you and your HEAD have detached from something else.
HEAD
是一个符号引用,指向您在提交历史中的任何位置。无论你走到哪里,做什么,它都会像影子一样跟着你。如果你做出承诺,HEAD
就会移动。如果你结帐的东西,HEAD
会移动。无论你做什么,如果你在提交历史中移动到了一个新的地方,HEAD
它就会跟着你一起移动。为了解决一个常见的误解:您无法将自己从HEAD
. 这不是分离的 HEAD 状态。如果你发现自己在想:“哦,不,我处于超脱的头部状态!我失去了我的头!” 记住,这是你的头。头是你。你还没有脱离 HEAD,你和你的 HEAD 已经脱离了别的东西。
What can HEAD attach to?
HEAD 可以附加什么?
HEAD
can point to a commit, yes, but typically it does not. Let me say that again. Typically HEAD
does not point to a commit.It points to a branch reference. It is attachedto that branch, and when you do certain things (e.g., commit
or reset
), the attached branch will move along with HEAD
. You can see what it is pointing to by looking under the hood.
HEAD
可以指向提交,是的,但通常不会。让我再说一遍。通常HEAD
不指向提交。它指向一个分支引用。它附着在那个分支上,当你做某些事情(例如,commit
或reset
)时,附着的分支会随着 移动HEAD
。通过查看引擎盖下的内容,您可以看到它所指向的内容。
cat .git/HEAD
Normally you'll get something like this:
通常你会得到这样的东西:
ref: refs/heads/master
Sometimes you'll get something like this:
有时你会得到这样的东西:
a3c485d9688e3c6bc14b06ca1529f0e78edd3f86
That's what happens when HEAD
points directly to a commit. This is called a detached HEAD, because HEAD
is pointing to something other than a branch reference. If you make a commit in this state, master
, no longer being attached to HEAD
, will no longer move along with you. It does not matter where that commit is. You could be on the same commit as your master branch, but if HEAD
is pointing to the commit rather than the branch, it is detached and a new commit will not be associated with a branch reference.
这就是HEAD
直接指向提交时发生的情况。这称为分离的 HEAD,因为HEAD
它指向的不是分支引用。如果您在此状态下进行提交master
,则不再附加到 的HEAD
将不再与您一起移动。提交在哪里并不重要。你可以和你的主分支在同一个提交上,但是如果HEAD
指向提交而不是分支,它是分离的,新提交将不会与分支引用相关联。
You can look at this graphically if you try the following exercise. From a git repository, run this. You'll get something slightly different, but they key bits will be there. When it is time to checkout the commit directly, just use whatever abbreviated hash you get from the first output (here it is a3c485d
).
如果您尝试以下练习,您可以以图形方式查看此内容。从 git 存储库,运行它。你会得到一些稍微不同的东西,但它们的关键位会在那里。当需要直接检出提交时,只需使用您从第一个输出中获得的任何缩写哈希(这里是a3c485d
)。
git checkout master
git log --pretty=format:"%h: %d" -1
# a3c485d: (HEAD -> master)
git checkout a3c485d -q # (-q is for dramatic effect)
git log --pretty=format:"%h: %d" -1
# a3c485d: (HEAD, master)
OK, so there is a small difference in the output here. Checking out the commit directly (instead of the branch) gives us a comma instead of an arrow. What do you think, are we in a detached HEAD state? HEAD is still referring to a specific revision that is associated with a branch name. We're still onthe master branch, aren't we?
好的,所以这里的输出略有不同。直接检查提交(而不是分支)会给我们一个逗号而不是箭头。你怎么看,我们是否处于分离的 HEAD 状态?HEAD 仍然指的是与分支名称相关联的特定修订。我们仍然对主分支,不是吗?
Now try:
现在尝试:
git status
# HEAD detached at a3c485d
Nope. We're in 'detached HEAD' state.
不。我们处于“分离头”状态。
You can see the same representation of (HEAD -> branch)
vs. (HEAD, branch)
with git log -1
.
你可以看到相同的表示(HEAD -> branch)
对(HEAD, branch)
用git log -1
。
In conclusion
综上所述
HEAD
is you. It points to whatever you checked out, wherever you are. Typically that is not a commit, it is a branch. If HEAD
doespoint to a commit (or tag), even if it's the same commit (or tag) that a branch also points to, you (and HEAD
) have been detached from that branch. Since you don't have a branch attached to you, the branch won't follow along with you as you make new commits. HEAD
, however, will.
HEAD
是你。它指向您签出的任何内容,无论您身在何处。通常这不是提交,而是分支。如果HEAD
确实指向提交(或标记),即使它与分支也指向的提交(或标记)相同,您(和HEAD
)也已与该分支分离。由于您没有附加分支,因此在您进行新提交时,该分支不会跟随您。HEAD
,然而,会。
回答by De Novo
HEAD
refers to the current commit that your working copy points to, i.e. the commit you currently have checked-out. From the official Linux Kernel documentation on specifying Git revisions:
HEAD
指的是您的工作副本指向的当前提交,即您当前已签出的提交。来自关于指定 Git 修订版的官方 Linux 内核文档:
HEAD
names the commit on which you based the changes in the working tree.
HEAD
命名您基于工作树中的更改的提交。
Note, however, that in the upcoming version 1.8.4 of Git, @
can also be used as a shorthand for HEAD
, as noted by Git contributor Junio C Hamano in his Git Blame blog:
但是请注意,在即将发布的 Git 1.8.4 版本中,@
也可以用作 的简写HEAD
,正如Git 贡献者 Junio C Hamano 在他的 Git Blame 博客中所指出的:
Instead of typing "HEAD", you can say "@" instead, e.g. "git log @".
不用输入“HEAD”,你可以用“@”代替,例如“git log @”。
Stack Overflow user VonCalso found some interesting information on why @
was chosen as a shorthand in his answer to another question.
Stack Overflow 用户VonC还发现了一些有趣的信息,说明为什么@
在他对另一个问题的回答中被选为速记。
Also of interest, in some environments it's not necessary to capitalize HEAD
, specifically in operating systems that use case-insensitive file systems, specifically Windows and OS X.
同样有趣的是,在某些环境中不需要大写HEAD
,特别是在使用不区分大小写的文件系统的操作系统中,特别是 Windows 和 OS X。
回答by onmyway133
Take a look at Creating and playing with branches
看看创建和玩弄分支
HEAD is actually a file whose contents determines where the HEAD variable refers:
HEAD 实际上是一个文件,其内容决定了 HEAD 变量引用的位置:
$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/refs/heads/master
35ede5c916f88d8ba5a9dd6afd69fcaf773f70ed
In this repository, the contents of the HEAD file refers to a second file named refs/heads/master. The file refs/heads/mastercontains the hash of the most recent commit on the master branch.
在这个存储库中, HEAD 文件的内容是指名为refs/heads/master的第二个文件。文件refs/heads/master包含主分支上最近提交的哈希值。
The result is HEAD points to the master branch commit from the .git/refs/heads/masterfile.
结果是 HEAD 指向.git/refs/heads/master文件中的 master 分支提交。
回答by mike
I'd just like to detail a few things in Greg Hewgil's accepted answer. According to the Git Pocket Guide
我只想详细说明 Greg Hewgil 接受的答案中的一些内容。根据Git 袖珍指南
Branch:
分支:
the branch itself is defined as all points reachable in the commit graph from the named commit (the “tip” of the branch).
分支本身被定义为从命名提交(分支的“提示”)在提交图中可到达的所有点。
HEAD:A special type of Ref
HEAD:一种特殊类型的 Ref
The special ref HEAD determines what branch you are on...
特殊的 ref HEAD 决定了你所在的分支......
Refs
参考资料
Git defines two kinds of references, or named pointers, which it calls “refs”:
- A simple ref, which points directly to an object ID (usually a commit or tag)
- A symbolic ref (or symref), which points to another ref (either simple or symbolic)
Git 定义了两种引用或命名指针,它称之为“refs”:
- 一个简单的 ref,它直接指向一个对象 ID(通常是一个提交或标签)
- 一个符号引用(或 symref),它指向另一个引用(简单的或符号的)
As Greg mentioned, HEAD can be in a "detached state". So HEAD can be either a simple ref (for a detached HEAD) or a symref.
正如 Greg 所提到的,HEAD 可以处于“分离状态”。所以 HEAD 可以是一个简单的引用(对于一个分离的 HEAD)或一个 symref。
if HEAD is a symbolic ref for an existing branch, then you are “on” that branch. If, on the other hand, HEAD is a simple ref directly naming a commit by its SHA-1 ID, then you are not “on” any branch, but rather in “detached HEAD” mode, which happens when you check out some earlier commit to examine.
如果 HEAD 是现有分支的符号引用,那么您“在”该分支上。另一方面,如果 HEAD 是一个简单的引用,直接通过其 SHA-1 ID 命名提交,那么您不是“在”任何分支上,而是处于“分离的 HEAD”模式,当您更早地检查时会发生这种情况承诺检查。
回答by Nataraj
I think 'HEAD' is current check out commit. In other words 'HEAD' points to the commit that is currently checked out.
我认为“HEAD”是当前签出提交。换句话说,'HEAD' 指向当前检出的提交。
If you have just cloned and not checked out I don't know what it points to, probably some invalid location.
如果您刚刚克隆并没有签出,我不知道它指向什么,可能是一些无效的位置。