Home > Back-end >  Git clone return master as default branch
Git clone return master as default branch

Time:06-30

When cloning a repository from a git server I'm getting master as the default branch, when the content of the HEAD file is ref: refs/heads/main. If I clone the repo from the bare repository stored in my local machine, everything works fine.

CodePudding user response:

After discussion, the OP Martin Daniel finds out in the comments the repository needs to be cloned with the git protocol 2 in order to have the proper default branch set in the local cloned repository.

git config --global protocol.version 2
git clone ...

That derives from the git protocol, which include a symref parameter

This parameterized capability is used to inform the receiver which symbolic ref points to which ref;

For example, "symref=HEAD:refs/heads/master" tells the receiver that HEAD points to master.
This capability can be repeated to represent multiple symrefs.

Servers SHOULD include this capability for the HEAD symref if it is one of the refs being sent.

Clients MAY use the parameters from this capability to select the proper initial branch when cloning a repository.

As noted in "How does Git's transfer protocol work", and commit 533e088 from Git 2.22.1 (Q2 2019)

The symref advertisement moved in v2 to be a part of the ls-refs command.

Before 2.22.1, the server side support for "git fetch" used to show incorrect value for the HEAD symbolic ref when the namespace feature is in use.


Original answer:

It depends on the Git server version.

The git config --global init.defaultBranch main dates from Git 2.28 (Q3 2020)

Before that, the Git init default template would list master as its default branch.

If I clone the repo from the bare repository stored in my local machine, everything works fine.

On a local machine, chances are your Git version is more recent than the one used on a server (On my RHEL 7.9 server, it is still a Git 1.8.5 from 2013!)

  •  Tags:  
  • git
  • Related