This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: git branch

Always default to main

face Josiah Wang

You can set the initial branch to main in the first place via git init.

user@MACHINE:~/someproject$ git init --initial-branch=main
Initialized empty Git repository in /home/user/someproject/.git/

It’s too much hassle to remember every time though.

Can I always default to main instead of master?

The answer is yes.

You will need to configure your local git client to always name your default branch as main whenever you create a new repo.

First, check that you have git version>=2.28 because this solution only works for these versions. If not, figure out how to upgrade your git client.

user@MACHINE:~/robot$ git --version
git version 2.32.0

Then do the following to configure your git client to always name your default branch as main in the future.

user@MACHINE:~/robot$ git config --global init.defaultBranch main

You can test whether this works by initialising a new git repository called branchtest (or whatever you like), and checking whether the default branch is main.

user@MACHINE:~/robot$ cd ..
user@MACHINE:~$ git init branchtest
Initialized empty Git repository in /home/user/branchtest/.git/
user@MACHINE:~$ cd branchtest
user@MACHINE:~/branchtest$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Looks like it is now officially main by default.