How To Make "main" The Default Branch

Stock photo by Yancy Min

👋 Hi, all! Best practices have changed! We're using main for our primary git branch now, because racism sucks, including terms that come from slavery like master. Here's how to update with minimal disruption to your workflow in three(ish) simple steps.*

  1. Use an up-to-date version of Git.
  2. Add a line to your .gitconfig.
  3. Change the default in GitHub.

1. Use an up-to-date version of Git

Linux

If the version that comes with your distro is ≥ 2.28, you're in good shape. Otherwise, head to git-scm.com/download/linux and install a newer version.

git --version

Or you can install it with Homebrew (yes, that Homebrew ⬇️) and get an up-to-date version by default. More on that in another post.

macOS

The version that Xcode installs is too old. That's ok though.

Since you probably have Homebrew installed already, you can simply...

brew install git

...to get up to date.

This version will be prioritized in your PATH over the old version, and your existing .gitconfig file will work just fine.

Windows

You probably installed git either through chocolatey or directly from git-scm.com.

If the former, just...

choco upgrade git

...and you should be golden.

If the latter - head back there and get yourself a newer copy.

2. Add a line to your .gitconfig

You can run this command to change the default branch to main for all new repos created on your local machine:

git config --global init.defaultBranch main

This appends the following lines to your .gitconfig:

[init]
	defaultBranch = main

3. Change the default on GitHub

You don't actually have to do anthing - they've already done it!

New repos automatically get created with main as the default branch. But if you'd like to change it to something cheeky like trunk, you can do so by going to /settings/repositories.


*The messy bits

  • Unfortunately, at the time of this writing, neither GitLab nor Gitea support globally changing the default branch for new repos. However, you can change this on a per-repo basis in the project settings.
  • Don't forget to change the "build from" branch in any pipelines you've created or they'll break.

FAQ

Is there anything special about master?

Nope! It's just been the historical default for a lot of things. But in the eyes of Git, a branch is a branch. The name is just another string.

Why are you FORCING me to CHANGE!? RAWR!!

I'm not. I can't force you to do anything. However...if your reasons for maintaing maintain the status quo are anything other than compatibility of legacy software...you should really reconsider whether they're good reasons.

Noah
DevOps Engineer, Aviator | They/Them
The Friendly Skies ✈️