What is HEAD in Git?

后端 未结 22 2010
野的像风
野的像风 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:40

    Take a look at Creating and playing with branches

    HEAD is actually a file whose contents determines where the HEAD variable refers:

    $ cat .git/HEAD
    ref: refs/heads/master
    $ cat .git/refs/heads/master
    35ede5c916f88d8ba5a9dd6afd69fcaf773f70ed
    

    In this repository, the contents of the HEAD file refers to a second file named refs/heads/master. The file refs/heads/master contains the hash of the most recent commit on the master branch.

    The result is HEAD points to the master branch commit from the .git/refs/heads/master file.

    enter image description here

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

    HEAD is just a special pointer that points to the local branch you’re currently on.

    From the Pro Git book, chapter 3.1 Git Branching - Branches in a Nutshell, in the section Creating a New Branch:

    What happens if you create a new branch? Well, doing so creates a new pointer for you to move around. Let’s say you create a new branch called testing. You do this with the git branch command:

    $ git branch testing 
    

    This creates a new pointer at the same commit you’re currently on

    enter image description here

    How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. Note that this is a lot different than the concept of HEAD in other VCSs you may be used to, such as Subversion or CVS. In Git, this is a pointer to the local branch you’re currently on. In this case, you’re still on master. The git branch command only created a new branch — it didn’t switch to that branch.

    enter image description here

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

    In addition to all definitions, the thing that stuck in my mind was, when you make a commit, GIT creates a commit object within the repository. Commit objects should have a parent ( or multiple parents if it is a merge commit). Now, how does git know the parent of the current commit? So HEAD is a pointer to the (reference of the) last commit which will become the parent of the current commit.

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

    I'd just like to detail a few things in Greg Hewgil's accepted answer. According to the Git Pocket Guide

    Branch:

    the branch itself is defined as all points reachable in the commit graph from the named commit (the “tip” of the branch).

    HEAD: A special type of Ref

    The special ref HEAD determines what branch you are on...

    Refs

    Git defines two kinds of references, or named pointers, which it calls “refs”:

    • A simple ref, which points directly to an object ID (usually a commit or tag)
    • A symbolic ref (or symref), which points to another ref (either simple or symbolic)

    As Greg mentioned, HEAD can be in a "detached state". So HEAD can be either a simple ref (for a detached HEAD) or a symref.

    if HEAD is a symbolic ref for an existing branch, then you are “on” that branch. If, on the other hand, HEAD is a simple ref directly naming a commit by its SHA-1 ID, then you are not “on” any branch, but rather in “detached HEAD” mode, which happens when you check out some earlier commit to examine.

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

    It feels like that HEAD is just a tag for the last commit that you checked out.

    This can be the tip of a specific branch (such as "master") or some in-between commit of a branch ("detached head")

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

    Take a look at http://git-scm.com/book/en/Git-Branching-What-a-Branch-Is

    Figure 3-5. HEAD file pointing to the branch you’re on.

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