为什么git分离HEAD存在? [英] Why does git detached HEAD even exist?

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

问题描述

我们的团队最近搬到了git,从svn和hg迁移过来。所以,当他们检查树的顶端时,根据GUI,他们倾向于检查特定的提交或本地分支,而不是新的跟踪分支,而不了解git的真实工作方式。

所以我的问题是为什么分离HEAD甚至存在?

为什么不能默认检查一个新的分支? (我对git的理解有限)



教育/培训绝对有帮助,但总会有人来git ...

你们如何?管理它?



顺便说一句,我知道如何解决它们。我已阅读了本网站上的许多帖子。

这对于知识共享以及您如何预防或管理更有用。

更新:

在阅读了下面提供的详细解释之后,我现在意识到我的问题应该是为什么git在检出新的远程分支时提供分离的HEAD?。无论如何,答案会给你一个很好的理解!

解决方案


[...]分离HEAD甚至存在?
为什么不能默认检查一个新的分支?

让我试试下面的比喻。如果你认为你的Git仓库是一个相册,记录你仓库的历史记录......




  • 你可以把分支想象成书签;他们会标记您的历史中的兴趣点,您可能会回溯到某个阶段的快照......如果只是出于怀旧:)

  • 您可以考虑 HEAD 引用为其中一个手指,使书在特定页面上保持打开状态。



现在,想象一下,如果您只允许在已有 书签的位置打开该书籍。这将是非常严格和笨拙的:您必须创建并使用许多书签才能访问您的历史记录的某些页面:

$ b



相反,Git允许您轻弹本书并打开它在你喜欢的任何页面上。然后,如果您注意到您感兴趣的特定快照,则可以随时为其创建新的书签(分支)。



简而言之,这就是为什么分离 - HEAD状态很有用。它允许您检出任何提交,即使没有分支当前指向的提交。如果您决定要将新的工作建立在提交的问题上,那么创建一个指向该提交的新分支是有意义的;但是,否则,创建一个新的分支将是矫枉过正。







为什么git会给出在检出一个新的远程分支时分离HEAD?

我猜你可能运行了

  git checkout< remote-branch> 

令人惊讶的是它将 HEAD 。您需要知道,Git区分您的存储库本地的


  • 远程跟踪分支但只是为了反映生活在远程存储库中的分支看起来像上次与服务器通信的时间;你不能在这些分支上工作;和

  • (纯粹)本地分支,可以使用。
  • 如果您只是运行

      git checkout< remote-branch> 

    没有本地分支供你玩,你会以detached结束 - code> HEAD 状态。你可以运行

      git checkout -b  <远程分支> 

    代替。这将创建并检出指向与远程分支相同的提交的新本地分支。没有分开 HEAD 然后。


    Our team recently moved to git, migrating from svn and hg. So when they checkout the tip of a tree, depending on the GUIs, they had a tendency to checkout a particular commit or local branch rather than a new tracking branch, without the understanding of how git really works.

    So my question is why does detached HEAD even exist?
    Why can't it be defaulted to always checking out a new branch? (with my limited understanding of git)

    Education/Training definitely helped but there is always someone new to git...
    How do you guys manage it?

    By the way, I know how to fix them. I've read through a lot of posts on this site.
    This is more for knowledge sharing and how you prevent or manage.

    Update:
    After reading the detailed explanations provided below, I now realized that my question should have been "Why does git give a detached HEAD when checking out a new remote branch?". Regardless, the answer will give you a very good understanding!

    解决方案

    [...] why does detached HEAD even exist? Why can't it be defaulted to always checking out a new branch?

    Let me try with the following metaphor. If you think of your Git repository as a photo album that chronicles the history of your repository...

    • You can think of branches as bookmarks; they mark points of interest in your history, snapshots you're likely to go back to, at some stage... if only out of nostalgia :)
    • You can think of the HEAD reference as one of your fingers, keeping the book open at a particular page.

    Now, imagine if you were allowed to open the book only where there is already a bookmark. That would be very restrictive and unwieldy: you would have to create and use many bookmarks just in order to visit certain pages of your history:

    Instead, Git allows you to flick through the book and open it on any page you fancy. Then, if you notice a particular snapshot that you've taken interest in, you can always create a new bookmark (branch) for it.

    In a nutshell, that's why detached-HEAD state is useful. It allows you to check out any commit, even one which no branch currently points to. If you decide that you'd like to base brand new work on the commit in question, then it would make sense to create a new branch that points to that commit; but, otherwise, creating a new branch would be overkill.


    Why does git give a detached HEAD when checking out a new remote branch?

    I'm guessing you probably run

    git checkout <remote-branch>
    

    and are suprised that it detaches the HEAD. You need to be aware that Git distinguishes between

    • remote-tracking branches, which are local to your repository, but are only meant to reflect what a branch living in a remote repository looked like the last time you communicated with the server; you cannot work on such branches; and
    • (purely) local branches, which can work on.

    If you simply run

    git checkout <remote-branch>
    

    no local branch for you to play with will be created, and you will end up in detached-HEAD state. You may want to run

    git checkout -b <new-local-branch> <remote-branch>
    

    instead. That will create and check out a new local branch pointing at the same commit as the remote branch. No detached HEAD then.

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

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