Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

What determines default branch after "git clone"?

My understanding is that the default branch of a cloned repository is to be whatever HEAD points to in the repo being cloned.

I now have a case where this is not true. My understanding is obviously flawed, so what does determine the default checkout branch when cloning a (bare) repo?

The last commit on that repo was a merge between the branch referenced in the bare repo's HEAD into the branch I'm getting as the checkout branch in the clone.

Running git remote show origin returns:

Fetch URL: ...
Push  URL: ...
HEAD branch (remote HEAD is ambiguous, may be one of the following):
  <bad-branch>
  live
Remote branches:
  ...

Bare repo uses Git version 1.8.2.1, client uses 1.7.12.4, transport is SSH.

Maybe the answer is actually this one here. This answer confirms it. If there is a choice of symbolic refs all pointing to the same revision as HEAD, the client will guess which branch to use.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...