What determines default branch after “git clone”?

Beginning with Git 1.8.5, the server will send the actual branch name that HEAD points to, in the “symref” capability. If you have both a client and server newer than Git 1.8.5, it will update HEAD correctly.

Prior to that, the client will guess what HEAD may have pointed to by comparing the object ID that HEAD (ultimately) points to with all the object IDs of all the branches. It prefers a branch named refs/heads/master: if both HEAD and master point to the same object ID, then clone will set the default branch in the new repository to master.

Otherwise, the first branch with a matching OID (when the branches are sorted alphanumerically) will be the default branch. If no branches have matching OIDs, then HEAD will be set directly to the object ID (ie, a detached HEAD).

Leave a Comment