Skip to content

get default branch name from remote repository

Nelson Ben requested to merge ls_remote_main_branch into master

commit 77192689 retrieves the branch name from the local repository, so it worked fine for new cloned repositories, but if you have an already cloned repository then you'll still get the old default branch name. It'll also fail if you clone a new repository and remote repository changes default branch name afterwards.

Fix this by using command:
git ls-remote --symref origin HEAD

which will retrieve the default branch name from the remote repository itself.

As a plus, git ls-remote is a plumbing command in git which means it has stable output so it's well suited for scripts.

Merge request reports