了解git中分离的HEAD [英] Understanding detached HEAD in git

查看:184
本文介绍了了解git中分离的HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习git,并正在 http://gitimmersion.com 上学习优秀的教程.

I am learning git and am following the excellent tutorials at http://gitimmersion.com.

在这些教程中,有一个blurb试图解释分离的HEAD,并指出:

In those tutorials, there is a blurb that tries to explain detached HEADs, and that states:

git中的分离式HEAD"消息仅表示HEAD(git的一部分,用于跟踪您当前工作目录应匹配的部分)直接指向提交而不是分支.只要您不切换到其他分支,就只会记住在此状态下提交的任何更改.一旦签出新的分支或标签,分离的提交将被丢失"(因为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.

对此有一些疑问:

  1. 什么是分离的HEAD?什么条件创建它?
  2. 为什么分离的HEAD指向特定的提交而不是分支?
  3. 拥有独立的HEAD时,是否始终需要创建一个新分支才能提交/推送其中的更改?为什么/为什么不呢?

提前谢谢!

推荐答案

如果您正在仓库中执行git checkout <SHA>,则将处于分离头".您不在分支上(提交可能在多个分支上).您将签到历史记录中的特定实例.

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.

重新安装底座时,也可能出现头部脱落.您已签出特定的提交.

A detached head can also occur when you are rebasing. You are checked out to a specific commit.

您将需要创建一个分支来提交/推送更改,因为您将创建处于不确定状态"的提交,除了SHA之外无法识别它们. Git将在垃圾回收期间删除提交,因为它不在分支中.

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.

在此页面上的独立头部"部分中进行查看以获取更多深度信息: http://git-scm.com/docs/git-checkout

Checkout the "Detached Head" Section on this page for more in depth information: http://git-scm.com/docs/git-checkout

这篇关于了解git中分离的HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆