Sub topic 8
Choosing the default branch name
This chapter assumes you have Git 2.28 or later, can run commands in a terminal, and know what a branch is. You should also know how the global scope differs from the local scope, which an earlier chapter covered.
What the setting does
A new repository starts with one branch. Git's built-in name for it is master. Many hosting services and teams now use main. The init.defaultBranch setting lets you choose the name that git init gives that first branch.
git config --global init.defaultBranch mainThis writes the following to ~/.gitconfig:
[init]
defaultBranch = mainIf you leave the setting unset, Git still works. Git 2.28 and later print a hint on every git init that suggests setting a name. Setting it once removes the hint and makes your repositories consistent.
How Git picks the name
When you run git init, Git checks three sources in order.
flowchart TD
A[git init] --> B{Was -b or --initial-branch given?}
B -- yes --> C[Use that name]
B -- no --> D{Is init.defaultBranch set?}
D -- yes --> E[Use the configured name]
D -- no --> F[Use master and print a hint]Rewrite this as the diagram block below.
The -b option wins for a single repository:
git init -b trunk my-trunk-projectVerifying the result
Create a throwaway repository and ask Git which branch HEAD points to. The branch has no commits yet, so git branch would list nothing. symbolic-ref reads HEAD directly instead.
git init demo
git -C demo symbolic-ref --short HEADThe second command prints main when your setting is active. To see where the value comes from, run:
git config --show-origin --get init.defaultBranchCommon errors
- Setting it with
--localand expecting it to work. Atgit inittime the new repository has no local file yet, so Git reads only the system and global scopes. Use--global. - Expecting existing repositories to change. The setting affects only repositories you create afterwards. To rename an existing branch, run
git branch -m master maininside it. - Expecting it to change what
git clonechecks out. A clone uses the branch that the remote reports as its default. The setting does not override it. - Using an older Git. Versions before 2.28 do not read the setting and ignore it without a warning. Check with
git --version.
Choosing a name
Git accepts any valid branch name. Pick a name your team and hosting service already use, because mismatched names cause confusing pushes and pull requests. main is the most common choice. If you work across many projects, set it globally once and use -b for the exceptions.
Exercise
You ran git config --local init.defaultBranch main inside an existing repository, then created a new repository with git init elsewhere. It starts on master. Why, and what is the fix?
Show answer
The local scope applies only to the repository whose .git/config holds it. The new repository never reads that file, and the global and system scopes have no value. Run git config --global init.defaultBranch main so every future git init uses main.