理解 git 中的分离 HEAD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19033709/
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
Understanding detached HEAD in git
提问by Schleis
I am learning git and am following the excellent tutorials at http://gitimmersion.com.
我正在学习 git 并且正在学习http://gitimmersion.com 上的优秀教程。
In those tutorials, there is a blurb that tries to explain detached HEADs, and that states:
在这些教程中,有一个简介试图解释分离的 HEAD,并指出:
A “detached HEAD” message in git just means that HEAD (the part of git that tracks what your current working directory should match) is pointing directly to a commit rather than a branch. Any changes that are committed in this state are only remembered as long as you don't switch to a different branch. As soon as you checkout a new branch or tag, the detached commits will be “lost” (because HEAD has moved). If you want to save commits done in a detached state, you need to create a branch to remember the commits.
git 中的“分离的 HEAD”消息仅表示 HEAD(git 中跟踪当前工作目录应匹配的内容的部分)直接指向提交而不是分支。只有在您不切换到不同分支时,才会记住在此状态下提交的任何更改。一旦你签出一个新的分支或标签,分离的提交就会“丢失”(因为 HEAD 已经移动了)。如果你想保存在分离状态下完成的提交,你需要创建一个分支来记住提交。
A few questions on this:
关于这个的几个问题:
- What exactly is a detached HEAD, and what conditions create it?
- Why is a detached HEAD pointing to a specific commit and not a branch?
- When you have a detached HEAD, do you always need to create a new branch to be able to commit/push the changes in it? Why/why not?
- 什么是分离的 HEAD,什么条件会产生它?
- 为什么分离的 HEAD 指向特定的提交而不是分支?
- 当你有一个分离的 HEAD 时,你是否总是需要创建一个新分支才能提交/推送其中的更改?为什么/为什么不?
Thanks in advance!
提前致谢!
回答by Schleis
If you are working in your repo and do git checkout <SHA>you will be in a "detached HEAD". You are not on a branch (the commit is likely to be on multiple branches). You are checked out to a specific instance in the history.
如果你在你的仓库中工作,git checkout <SHA>你将处于“分离的 HEAD”中。您不在一个分支上(提交很可能在多个分支上)。您已检出历史记录中的特定实例。
A detached head can also occur when you are rebasing. You are checked out to a specific commit.
重新定位时也可能发生分离的头部。你被签出到一个特定的提交。
You would need to create a branch in order to commit/push changes because you would be creating commits that would be "in limbo" with no way to identify them other than the SHA. Git will remove the commit during its garbage collection because of it not being on a branch.
您需要创建一个分支以提交/推送更改,因为您将创建“处于不确定状态”的提交,除了 SHA 之外无法识别它们。Git 将在其垃圾收集期间删除提交,因为它不在分支上。
Checkout the "Detached Head" Section on this page for more in depth information: http://git-scm.com/docs/git-checkout
查看此页面上的“分离头”部分以获取更深入的信息:http: //git-scm.com/docs/git-checkout

