git 中的 HEAD 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2529971/
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 the HEAD in git?
提问by e-satis
There seems to be a difference between the last commit, the HEAD and the state of the file I can see in my directory.
我在我的目录中可以看到的最后一次提交、HEAD 和文件状态之间似乎存在差异。
What is HEAD, what can I do with it and what mistake should I avoid?
什么是 HEAD,我可以用它做什么,我应该避免什么错误?
采纳答案by poke
HEAD is a reference to the last commit in the currently checked-out branch.
HEAD 是对当前签出分支中最后一次提交的引用。
There is a small exception to this, which is the detached HEAD. A detached HEADis the situation you end up in whenever you check out a commit(or tag) instead of a branch. In this case, you have to imagine this as a temporary branchwithout a name; so instead of having a named branch reference, we onlyhave HEAD. It will still allow you to make commits (which will update HEAD), so the above short definition is still true if you think of a detached HEAD as a temporary branch without a name.
对此有一个小例外,即分离的 HEAD。一个分离的头是你,只要你签出在结束了的情况提交,而不是一个分支(或标签)。在这种情况下,你必须把它想象成一个没有名字的临时分支;所以我们没有命名分支引用,我们只有HEAD。它仍然允许您进行提交(这将更新 HEAD),因此如果您将分离的 HEAD 视为没有名称的临时分支,则上述简短定义仍然适用。
回答by Cascabel
HEAD
is a ref (reference) to the currently checked out commit.
HEAD
是对当前签出提交的引用(引用)。
In normal states, it's actually a symbolic ref to the branch you have checked out - if you look at the contents of .git/HEAD you'll see something like "ref: refs/heads/master". The branch itself is a reference to the commit at the tip of the branch. Therefore, in the normal state, HEAD
effectively refers to the commit at the tip of the current branch.
在正常状态下,它实际上是您签出的分支的符号引用 - 如果您查看 .git/HEAD 的内容,您会看到类似“ref: refs/heads/master”的内容。分支本身是对分支尖端提交的引用。因此,在正常状态下,HEAD
有效地指的是当前分支顶端的提交。
It's also possible to have a "detached HEAD". This happens when you check out something besides a (local) branch, like a remote branch, a specific commit, or a tag. The most common place to see this is during an interactive rebase, when you choose to edit a commit. In detached HEAD state, your HEAD is a direct reference to a commit - the contents of .git/HEAD will be a SHA1 hash.
也可以有一个“分离的 HEAD”。当您检出(本地)分支之外的其他内容时会发生这种情况,例如远程分支、特定提交或标签。最常见的地方是在交互式变基期间,当您选择编辑提交时。在分离的 HEAD 状态下,您的 HEAD 是对提交的直接引用 - .git/HEAD 的内容将是 SHA1 哈希。
Generally speaking, HEAD is just a convenient name to mean "what you have checked out" and you don't really have to worry much about it. Just be aware of what you have checked out, and remember that you probably don't want to commit if you're not on a branch (detached HEAD state) unless you know what you're doing (e.g. are in an interactive rebase).
一般来说,HEAD 只是一个方便的名字,意思是“你检查过的东西”,你真的不必太担心。请注意您已签出的内容,并记住,如果您不在分支上(分离的 HEAD 状态),您可能不想提交,除非您知道自己在做什么(例如,在交互式变基中) .
回答by Honey
I always thought HEAD~5
means GO to 5 commits before. But it doesn't carry the GO part of the command. It only carries the reference/'where to' partof the command.
我一直认为HEAD~5
意味着 GO 到 5 次提交之前。但它不携带命令的 GO 部分。它只携带命令的引用/“到哪里”部分。
In layman terms it's used to answer the question of: WHEREshould I go? To which commit?
在外行人而言它用来回答的问题:在哪里我应该去?向哪个提交?
HEAD
means (the reference to the) current commitHEAD~1
means (the reference to) 1 commit beforeHEAD~
ALSO means (the reference to) 1 commit beforeHEAD~87
means (the reference to) 87 commits before
HEAD
表示(对)当前提交的引用HEAD~1
表示(引用)之前的 1 次提交HEAD~
也意味着(引用)之前的 1 次提交HEAD~87
意味着(引用)87 之前提交
Usage:
用法:
git checkout HEAD~1
will actually GO/checkout to 1 commit/reference beforegit reset HEAD~3
will uncommit your last 3 commits — without removing the changes, ie you can see all the changes made in the last 3 commits together, remove anything you don't like or add onto it and then commit them all again.git diff HEAD~3
for checking changes in the last 3 commits
git checkout HEAD~1
实际上会在 GO/checkout 到 1 个提交/引用之前git reset HEAD~3
将取消提交您的最后 3 次提交 — 不删除更改,即您可以一起查看最近 3 次提交中所做的所有更改,删除您不喜欢的任何内容或添加到其中的任何内容,然后再次提交所有内容。git diff HEAD~3
用于检查最近 3 次提交的更改
回答by Suhail Gupta
HEAD pointer in Git
Git 中的 HEAD 指针
Git maintains a reference variable called HEAD. And we call this variable a pointer, because its purpose is to reference, or point to, a specific commit in the repository. As we make new commits the pointer is going to change or move to point to a new commit. HEAD always points to the tip of the current branch in our repository. Now, this has to do with our repository, not our staging index, or our working directory.
Git 维护一个名为 HEAD 的引用变量。我们称这个变量为指针,因为它的目的是引用或指向存储库中的特定提交。当我们进行新的提交时,指针将改变或移动以指向新的提交。HEAD 始终指向我们存储库中当前分支的尖端。现在,这与我们的存储库有关,而不是我们的暂存索引或我们的工作目录。
Another way to think of it is the last state of our repository or what was last checked out, and because it's where the repository left off or the last state, you can also say that the HEAD points to the parent of the next commit or it's where commit writing is going to take place.
另一种思考方式是我们存储库的最后状态或上次检出的内容,因为它是存储库停止的位置或最后状态,您也可以说 HEAD 指向下一次提交的父级,或者它是提交写入将发生的地方。
I think a good metaphor to think about this is the playback and record head on a cassette tape recorder. As we start recording audio, the tape moves past the head, and it records onto it. when we press Stop the place where that record head is stopped is the place it'll start recording again when we press Record a second time.Now we can move around, we can move the head to different places, but wherever the head is positioned when we hit Record again that's where it's going to start recording.
我认为一个很好的比喻是磁带录音机上的播放和录音磁头。当我们开始录制音频时,磁带经过磁头,然后记录到磁头上。当我们按下停止键时,录音头停止的地方就是当我们第二次按下录音时它会再次开始录音的地方。当我们再次点击 Record 时,它就会开始录制。
The HEAD pointer in Git is very similar, it points at the place where we're going to start recording next. It's the place where we left off in our repository for the things that we've committed.
Git 中的 HEAD 指针非常相似,它指向我们接下来要开始录制的位置。这是我们在我们的存储库中为我们已经提交的事情留下的地方。
回答by Testilla
In simple terms, HEAD is a reference to the last commit in the currently check-out branch.
简单来说,HEAD 是对当前检出分支中最后一次提交的引用。
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
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。