且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当从git裸仓库克隆时,什么决定了默认分支

更新时间:2023-02-14 22:08:07

旧版本的git在获取和克隆时不会发送默认分支。相反,他们发送当前HEAD引用的对象ID以及其他引用的对象ID。如果HEAD的对象ID与另一个引用的对象ID相同,则会将HEAD设置为指向该引用。 (如果没有匹配,你将处于分离HEAD状态。)



消歧逻辑很模糊,但是如果你有多个分支指向同样的承诺。它会喜欢一个名为master的分支,然后它会简单地选择它首先看到的分支。



别笑,我是认真的。

这已在 Git 1.8.4.3 中解决。


I'm trying to change the default branch when cloning from a git bare repo using git 1.8.3.1 from yum install. I had always assumed that the way to do this would be to change the reference inside the HEAD file of the remote repo, and several SO answers seem to confirm this.

However, I'm finding the value of HEAD in the remote bare repo to have no effect. Even though my remote repo has this value in HEAD:

ref: refs/heads/secondbranch

I'm still defaulted to firstbranch branch when I clone! Any ideas what's going on here? Where else might this info be stored?

Old versions of git do not send the "default branch" when fetching and cloning. Instead, they send the object ID of the current HEAD reference, as well as the object ID of the other references. If the object ID of HEAD is the same as the object ID of another reference, it will set HEAD to point to that reference. (If there was no match, you will be in a detached HEAD state.)

The disambiguation logic is fuzzy, though, if you have more than one branch pointing to the same commit. It will prefer a branch named master, then it will simply pick the one it saw first.

Stop laughing, I'm serious.

This was fixed in Git 1.8.4.3.