How to Rename the Default Git Branch and Why You’d Want To

If you’ve been using Git for a while, you’ve probably come across the term master. You might’ve seen it when you run git status or git branch.

It’s a somewhat old-fashioned phrase and you may well be wondering where it comes from or how you can change it. Whether you’re a command-line user or a fan of GitHub, you can change branch names and even set the default branch name to something appropriate to your needs.

What Is the Default Branch?

Every brand new Git repository has a default branch, even if it doesn’t really branch off anything! Branches are simply commit references, and there’s always a HEAD reference pointing to the current branch.

Historically, Git has named that default branch master. Although you can rename it, many people stick with the default, so you’ll see lots of projects using master as their default branch.

Branch Naming and Why Master Is Being Phased Out

Recent versions of Git (2.28 and later) produce the following hint when you create a new repository using git init:

Using ‘master’ as the name for the initial branch. This default branch name is subject to change. To configure the initial branch name to use in all of your new repositories, which will suppress this warning, call: git config –global init.defaultBranch Names commonly chosen instead of ‘master’ are ‘main’, ‘trunk’ and ‘development’. The just-created branch can be renamed via this command: git branch -m

The master/slave terminology has a long history in computing, especially in reference to hardware such as disk drives. Other version control systems such as BitKeeper, a predecessor to Git, have also used the term. However, the term has become somewhat outdated for its association with colonialism.

The Git maintainers have worked with the wider development community to address these concerns. Aside from any offense it may cause, master isn’t a particularly descriptive name anyway. It implies a relationship with some other, unnamed entity, but it doesn’t represent the unique status of the initial branch. Many people consider the name main to do a better job of describing this branch and its typical usage.

The name main is short, translates easily, and is in common use. The fact that it begins with the same two letters as master may help if you need to retrain your muscle memory.

How to Make the Change Using Command Line Git

As Git itself explains, you can configure the default branch name with the following command:

git config --global init.defaultBranch main

The –global ensures this setting applies to all repositories created by the current user.

See also: How to Install and Configure Git on Linux

Once set, new repositories will use the new default name:

$ git init
Initialized empty Git repository in /private/tmp/bar/.git/
$ git status -sb
## No commits yet on main

Note that you can also rename a branch at any time using the -m option, e.g.

git branch -m main

One day, Git may make this change for you, by using the name main by default, for all new repositories. It’s still useful to know how to change it yourself, and you may have a personal or team preference for a name other than main.

How to Set the Default Branch Name in GitHub

Branches created on GitHub are now automatically named main instead of master. However, you can still change the name of this default by following these steps:

  1. Open Settings via your profile photo in the top-right.
  2. Click Repositories in the left-hand menu.
  3. Under the Repository default branch section, choose an alternative name.
  4. Click Update.

Related: What Is GitHub? An Introduction to Its Basic Features

Take Control of Git’s Default Branch Name

Git has historically named its default branch master, but you don’t have to keep it that way!

Whether you think an alternative is more inclusive, easier to understand, or simply shorter to type, it’s easy to change. The default branch is just one tiny part of Git’s enormous repertoire. To take your Git skill from beginner to advanced, check out our comprehensive guide next.

Source: makeuseof.com

Related posts

These 8 Tools Will Help You Learn the Ropes of Music Production

GPT-4 Is Now Free for Everyone, but There Are Still 6 Reasons to Keep Using ChatGPT Plus

Anker EverFrost Dual-Zone Powered Cooler 50 Review: Flexible Chilling on Demand