What is HEAD in Git?

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

    These two may confusing you:

    head

    Pointing to named references a branch recently submitted. Unless you use the package reference , heads typically stored in $ GIT_DIR/refs/heads/.

    HEAD

    Current branch, or your working tree is usually generated from the tree HEAD is pointing to. HEAD must point to a head, except you are using a detached HEAD.

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

    To quote other people:

    A head is simply a reference to a commit object. Each head has a name (branch name or tag name, etc). By default, there is a head in every repository called master. A repository can contain any number of heads. At any given time, one head is selected as the “current head.” This head is aliased to HEAD, always in capitals".

    Note this difference: a “head” (lowercase) refers to any one of the named heads in the repository; “HEAD” (uppercase) refers exclusively to the currently active head. This distinction is used frequently in Git documentation.

    Another good source that quickly covers the inner workings of git (and therefor a better understanding of heads/HEAD) can be found here. References (ref:) or heads or branches can be considered like post-it notes stuck onto commits in the commit history. Usually they point to the tip of series of commits, but they can be moved around with git checkout or git reset etc.

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

    Git is all about commits.
    And Head points to the commit which you currently checked out.

    $ git cat-file -t HEAD
    commit
    

    Whenever you checkout a branch, the HEAD points to the latest commit on that branch. Contents of HEAD can checked as below (for master branch):

    $ cat .git/refs/heads/master
      b089141cc8a7d89d606b2f7c15bfdc48640a8e25
    
    0 讨论(0)
  • 2020-11-22 10:49

    I am also still figuring out the internals of git, and have figured out this so far:

    Let's say the current branch is master.

    1. HEAD is a file in your .git/ directory that normally looks something like this:
    % cat .git/HEAD
    ref: refs/heads/master
    
    1. refs/heads/master is itself a file that normally has the hash value of the latest commit of master:
    % cat .git/refs/heads/master 
    f342e66eb1158247a98d74152a1b91543ece31b4
    
    1. If you do a git log, you will see that this is the latest commit for master:
    % git log --oneline 
    f342e66 (HEAD -> master,...) latest commit
    fa99692 parent of latest commit
    

    So my thinking is the HEAD file is a convenient way to track the latest commit, instead of remembering long hash values.

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

    HEAD refers to the current commit that your working copy points to, i.e. the commit you currently have checked-out. From the official Linux Kernel documentation on specifying Git revisions:

    HEAD names the commit on which you based the changes in the working tree.

    Note, however, that in the upcoming version 1.8.4 of Git, @ can also be used as a shorthand for HEAD, as noted by Git contributor Junio C Hamano in his Git Blame blog:

    Instead of typing "HEAD", you can say "@" instead, e.g. "git log @".

    Stack Overflow user VonC also found some interesting information on why @ was chosen as a shorthand in his answer to another question.

    Also of interest, in some environments it's not necessary to capitalize HEAD, specifically in operating systems that use case-insensitive file systems, specifically Windows and OS X.

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

    Head points to the tip of the currently checked out branch.

    In your repository, there is a .git folder. Open the file in this location: .git\refs\heads. The (sha-1 hash) code in that file (master in most cases) will be the most recent commit, i.e the one seen in the output of the command git log. More info on the .git folder: http://gitready.com/advanced/2009/03/23/whats-inside-your-git-directory.html

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