What is HEAD in Git?

后端 未结 22 1994
野的像风
野的像风 2020-11-22 10:02

You see the Git documentation saying things like

The branch must be fully merged in HEAD.

But what is Git HEAD exac

相关标签:
22条回答
  • 2020-11-22 10:31

    As a concept, the head is the latest revision in a branch. If you have more than one head per named branch you probably created it when doing local commits without merging, effectively creating an unnamed branch.

    To have a "clean" repository, you should have one head per named branch and always merge to a named branch after you worked locally.

    This is also true for Mercurial.

    0 讨论(0)
  • 2020-11-22 10:32

    I think 'HEAD' is current check out commit. In other words 'HEAD' points to the commit that is currently checked out.

    If you have just cloned and not checked out I don't know what it points to, probably some invalid location.

    0 讨论(0)
  • 2020-11-22 10:32

    HEAD actually is just a file for storing current branch info

    and if you use HEAD in your git commands you are pointing to your current branch

    you can see the data of this file by cat .git/HEAD

    0 讨论(0)
  • 2020-11-22 10:36

    A branch is actually a pointer that holds a commit ID such as 17a5. HEAD is a pointer to a branch the user is currently working on.

    HEAD has a reference filw which looks like this:

    ref:

    You can check these files by accessing .git/HEAD .git/refs that are in the repository you are working in.

    0 讨论(0)
  • 2020-11-22 10:39

    Assuming it is not a special case called "detached HEAD", then, as stated in the O'Reilly Git book, 2nd edtion, p.69, HEAD means:

    HEAD always refers to the most recent commit on the current branch. When you change branches, HEAD is updated to refer to the new branch’s latest commit.

    so

    HEAD is the "tip" of the current branch.

    Note that we can use HEAD to refer to the most recent commit, and use HEAD~ as the commit before the tip, and HEAD~~ or HEAD~2 as the commit even earlier, and so forth.

    0 讨论(0)
  • 2020-11-22 10:39

    After reading all of the previous answers, I still wanted more clarity. This blog at the official git website http://git-scm.com/blog gave me what I was looking for:

    The HEAD: Pointer to last commit snapshot, next parent

    The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory. That also means it will be the parent of the next commit you do. It's generally simplest to think of it as HEAD is the snapshot of your last commit.

    0 讨论(0)
提交回复
热议问题