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.

- 23
- 5
1 Answers
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 thatHEAD
points tomaster
.
This capability can be repeated to represent multiplesymref
s.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!)

- 1,262,500
- 529
- 4,410
- 5,250
-
The server also runs on my machine, the only difference is that when I clone directly from the bare repo it doesn't go through an HTTP request. By the way, I'm using Git 2.36.1 – Martin Daniel Jun 29 '22 at 08:14
-
@MartinDaniel I suppose that your server on the same machine is also using 2.36.1? – VonC Jun 29 '22 at 09:02
-
yes I am using the same – Martin Daniel Jun 29 '22 at 09:18
-
@MartinDaniel OK. Would `git remote set-head origin main` be enough? – VonC Jun 29 '22 at 09:20
-
No, I ran the command and the result was `error: Not a valid ref: refs/remotes/origin/main` – Martin Daniel Jun 29 '22 at 09:23
-
@MartinDaniel DOes your remote repository (the one on server) include a `main` branch? `git ls-remote` will show you the remote branches. `git branch -avv` will list the fetched remote branches. If in both cases there is no `main` branch... that would explain why cloning your remote repo result in `master` as the default branch. – VonC Jun 29 '22 at 09:26
-
Yes it had the branch main, it was actually initialized with it `git init -b main --bare` was the command, then set the default branch to be main executing `git symbolic-ref HEAD refs/heads/main`. Finally, the answer was that the server needed to be executing the clone commands using the git protocol 2. Thanks for all the precious answers! – Martin Daniel Jun 29 '22 at 10:19
-
1@MartinDaniel Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Jun 29 '22 at 10:54