What is HEAD in Git?

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

提交回复
热议问题