Git 中的 HEAD 和 ORIG_HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/964876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:34:40  来源:igfitidea点击:

HEAD and ORIG_HEAD in Git

git

提问by collimarco

What do these symbols refer to and what do they mean?

这些符号指的是什么,它们是什么意思?

(I can't find any explanation in official documentation)

(我在官方文档中找不到任何解释)

回答by Jakub Nar?bski

HEADis (direct or indirect, i.e. symbolic) reference to the current commit. It is a commit that you have checked in the working directory (unless you made some changes, or equivalent), and it is a commit on top of which "git commit" would make a new one. Usually HEADis symbolic reference to some other named branch; this branch is currently checked out branch, or current branch. HEADcan also point directly to a commit; this state is called "detached HEAD", and can be understood as being on unnamed, anonymous branch.

HEAD是(直接或间接,即符号)对当前提交的引用。这是您已在工作目录中签入的提交(除非您进行了一些更改或等效),并且它是一个提交,“git commit”将在其之上创建一个新的提交。通常HEAD是对其他命名分支的符号引用;此分支当前是签出分支,或当前分支。HEAD也可以直接指向一个提交;这种状态称为“分离的 HEAD”,可以理解为处于未命名的匿名分支上。

And @alone is a shortcut for HEAD, since Git 1.8.5

并且@单独是 的快捷方式HEAD,因为 Git 1.8.5

ORIG_HEADis previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them. It is less useful now that Git has reflog: HEAD@{1}is roughly equivalent to ORIG_HEAD(HEAD@{1}is always last value of HEAD, ORIG_HEADis last value of HEADbefore dangerous operation).

ORIG_HEAD是 的先前状态HEAD,由可能具有危险行为的命令设置,以便于恢复它们。现在 Git 有 reflog 的用处不大:HEAD@{1}大致相当于ORIG_HEADHEAD@{1}总是最后一个值HEADORIG_HEADHEAD危险操作之前的最后一个值)。

For more information read git(1) manpage, Git User's Manual, the Git Community Bookand Git Glossary

有关更多信息,请阅读git(1) 联机帮助页Git 用户手册Git 社区手册Git 词汇表

回答by VonC

From git reset

git 重置

"pull" or "merge" always leaves the original tip of the current branch in ORIG_HEAD.

“拉”或“合并”总是将当前分支的原始尖端留在ORIG_HEAD.

git reset --hard ORIG_HEAD

Resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the branch to that commit.

硬重置它会使您的索引文件和工作树恢复到该状态,并将分支的尖端重置为该提交。

git reset --merge ORIG_HEAD

After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory. Running "git reset --hard ORIG_HEAD" will let you go back to where you were, but it will discard your local changes, which you do not want. "git reset --merge" keeps your local changes.

检查合并的结果后,您可能会发现其他分支的更改并不令人满意。运行“ git reset --hard ORIG_HEAD”会让您回到原来的位置,但它会丢弃您不想要的本地更改。" git reset --merge" 保持您的本地更改。



Before any patches are applied, ORIG_HEAD is set to the tip of the current branch.
This is useful if you have problems with multiple commits, like running 'git am' on the wrong branch or an error in the commits that is more easily fixed by changing the mailbox (e.g. +errors in the "From:" lines).

In addition, merge always sets '.git/ORIG_HEAD' to the original state of HEAD so a problematic merge can be removed by using 'git reset ORIG_HEAD'.

在应用任何补丁之前,ORIG_HEAD 被设置为当前分支的尖端。
如果您在多次提交时遇到问题,例如git am在错误的分支上运行 ' ' 或提交中的错误(通过更改邮箱更容易修复)(例如,“发件人:”行中的 + 错误),这将非常有用。

此外,merge 总是将 ' .git/ORIG_HEAD'设置为 HEAD 的原始状态,因此可以使用 ' git reset ORIG_HEAD'删除有问题的合并。



Note: from here

注意:从这里

HEAD is a moving pointer. Sometimes it means the current branch, sometimes it doesn't.

So HEAD is NOTa synonym for "current branch" everywhere already.

HEAD means "current" everywherein git, but it does not necessarily mean "current branch" (i.e. detached HEAD).

But it almost always means the "current commit".
It is the commit "git commit" builds on top of, and "git diff --cached" and "git status" compare against.
It means the current branch only in very limited contexts (exactly when we want a branch name to operate on --- resetting and growing the branch tip via commit/rebase/etc.).

Reflog is a vehicle to go back in time and time machines have interesting interaction with the notion of "current".

HEAD@{5.minutes.ago}could mean "dereference HEAD symref to find out what branch we are on RIGHT NOW, and then find out where the tip of that branch was 5 minutes ago".
Alternatively it could mean "what is the commit I would have referred to as HEAD 5 minutes ago, e.g. if I did "git show HEAD" back then".

HEAD 是一个移动指针。有时它意味着当前分支,有时它不是。

所以 HEAD 已经不是“当前分支”的同义词了。

HEAD在 git 中的任何地方都表示“当前”,但不一定表示“当前分支”(即分离的 HEAD)。

但它几乎总是意味着“当前提交”。
它是git commit构建在“ ”之上的提交,并且“ git diff --cached”和“ git status”比较。
这意味着当前分支仅在非常有限的上下文中(正是当我们想要一个分支名称进行操作时——通过 commit/rebase/etc. 重置和增长分支提示)。

Reflog 是一种回到过去的工具,时间机器与“当前”的概念有有趣的互动。

HEAD@{5.minutes.ago}可能意味着“取消引用 HEAD symref 以找出我们现在所在的分支,然后找出 5 分钟前该分支的尖端在哪里”。
或者,它可能意味着“什么是我在 5 分钟前称为 HEAD 的提交,例如,如果我当时做了“git show HEAD””。



git1.8.4 (July 2013)introducesintroduceda new notation!
(actually, it will be for 1.8.5 or 1.9, Q4 2013: reintroduced with commit 9ba89f4)

git1.8.4(2013 年 7 月)介绍引入了一个新的符号!
(实际上,它将用于 1.8.5 或1.9,2013 年第四季度:通过提交 9ba89f4重新引入)

Instead of typing four capital letters "HEAD", you can say "@" now,
e.g. "git log @".

HEAD@现在可以说“ ”,而不是键入四个大写字母“ ” ,
例如“ git log @”。

See commit cdfd948

提交 cdfd948

Typing 'HEAD' is tedious, especially when we can use '@' instead.

The reason for choosing '@' is that it follows naturally from the ref@opsyntax (e.g. HEAD@{u}), except we have no ref, and no operation, and when we don't have those, it makes sens to assume 'HEAD'.

So now we can use 'git show @~1', and all that goody goodness.

Until now '@' was a valid name, but it conflicts with this idea, so let's make it invalid. Probably very few people, if any, used this name.

键入 ' HEAD' 很乏味,尤其是当我们可以使用 ' @' 代替时。

选择 ' @'的原因是它自然遵循ref@op语法(例如HEAD@{u}),除了我们没有 ref 和操作,当我们没有这些时,假设 ' HEAD'是有意义的。

所以现在我们可以使用 ' git show @~1' 以及所有这些好东西。

到目前为止,' @' 是一个有效的名称,但它与这个想法相冲突,所以让我们将其设为无效。可能很少有人,如果有的话,使用这个名字。



A blog post during the 1.8.4-rc3 period(14th, August 2013) announced that this feature was reverted and delayed (Thank you Cupcakefor the heads-up).
Again, it is introduced again with commit 9ba89f4(Sept. 2013).

一个在1.8.4-RC3期博客文章(14日,2013年8月)宣布,该功能被恢复和延迟(谢谢你的蛋糕抬头)。
同样,它再次通过提交 9ba89f4(2013 年 9 月)引入 。

See commit 2c2b664:

请参阅提交 2c2b664

Revert "Add new @shortcut for HEAD"

还原“@HEAD”添加新快捷方式

This reverts commit cdfd948, as it does not just apply to "@" (and forms with modifiers like @{u}applied to it), but also affects e.g. "refs/heads/@/foo", which it shouldn't.

The basic idea of giving a short-hand might be good, and the topic can be retried later, but let's revert to avoid affecting existing use cases for now for the upcoming release.

这将恢复提交cdfd948,因为它不仅适用于“ @”(以及具有类似修饰符的形式@{u}),还会影响例如“ refs/heads/@/foo”,它不应该影响。

提供简写的基本想法可能很好,并且可以稍后重试该主题,但让我们恢复一下以避免影响即将发布的版本的现有用例。

回答by ynimous

My understanding is that HEAD points the current branch, while ORIG_HEAD is used to store the previous HEAD before doing "dangerous" operations.

我的理解是 HEAD 指向当前分支,而 ORIG_HEAD 用于在进行“危险”操作之前存储之前的 HEAD。

For example git-rebase and git-am record the original tip of branch before they apply any changes.

例如 git-rebase 和 git-am 在应用任何更改之前记录分支的原始提示。

回答by Nathan Chappell

From man 7 gitrevisions:

来自man 7 gitrevisions

HEAD names the commit on which you based the changes in the working tree. FETCH_HEAD records the branch which you fetched from a remote repository with your last git fetch invocation. ORIG_HEAD is created by commands that move your HEAD in a drastic way, to record the position of the HEAD before their operation, so that you can easily change the tip of the branch back to the state before you ran them. MERGE_HEAD records the commit(s) which you are merging into your branch when you run git merge. CHERRY_PICK_HEAD records the commit which you are cherry-picking when you run git cherry-pick.

HEAD 命名您在工作树中基于更改的提交。FETCH_HEAD 记录您使用上次 git fetch 调用从远程存储库获取的分支。ORIG_HEAD 是由以剧烈方式移动 HEAD 的命令创建的,以记录 HEAD 操作前的位置,以便您可以轻松地将分支的尖端更改回运行它们之前的状态。MERGE_HEAD 记录您在运行 git merge 时合并到分支中的提交。CHERRY_PICK_HEAD 记录您在运行 git cherry-pick 时挑选的提交。