Git init still gives me master instead of the default branch name (main) in my config

Viewed 2249

enter image description here

I'm new in using Linux Ubuntu and I am setting up my git. I have already used git in windows. I'm trying to set my git default branch into main because that's what I'm used to in windows and all of my branch in other repository is also main. I've been trying to change it for a while now but git init still gives a default branch of master. I also tried giving it a default branch name other than main but it still gives master.

3 Answers

init.defaultBranch is a new config key in Git 2.28, so your 2.25 doesn't recognize it.

You can follow this answer on Ask Ubuntu to install the latest version from Ubuntu Git Maintainers so that config is honored.

Besides iBug's correct "upgrade Git" answer, note that you can run git init and then immediately just run git checkout -b main to switch the unborn branch name to main. This works regardless of how old your Git is, all the way back to the versions before the ancient ones in some RedHat/CentOS distributions.

With Git 2.35 (Q1 2022), "git var GIT_DEFAULT_BRANCH"(man)" is a way to see what name is used for the newly created branch if "git init"(man) is run.

See commit e06c9e1 (03 Nov 2021) by Thomas Weißschuh (t-8ch).
(Merged by Junio C Hamano -- gitster -- in commit 03194a1, 10 Dec 2021)

var: add GIT_DEFAULT_BRANCH variable

Helped-by: Johannes Schindelin
Helped-by: Eric Sunshine
Helped-by: Ævar Arnfjörð Bjarmason
Signed-off-by: Thomas Weißschuh

Introduce the logical variable GIT_DEFAULT_BRANCH which represents the the default branch name that will be used by git init".

Currently this variable is equivalent to

git config init.defaultbranch || 'master'

This however will break if at one point the default branch is changed as indicated by default_branch_name_advice in refs.c.

By providing this command ahead of time users of git can make their code forward-compatible.

git var now includes in its man page:

GIT_DEFAULT_BRANCH

The name of the first branch created in newly initialized repositories.

Related