How To Set Default Branch Name In Git & Github(Master To Main)

The git is a distributed version control system that primarily uses the concept of branching providing an isolated way to make changes without any impact to existing data. If you have no idea about branching then I suggest you take a look at the following guide.

Related Article : Git local and remote branches

When you initialize a git repository and create your first commit the default branch called ‘master’ is created.

$ mkdir /tmp/nixzie-default-branch
$ cd /tmp/nixzie-default-branch
$ git init 
Initialized empty Git repository in /tmp/nixzie-default-branch

$ echo "sample commit for default branch test" > nixzie-default-branch.txt
$ git add .
$ git commit -m "sample commit - default branch"

$ git branch 
master

The default branch doesn’t need to be named master. It can be named anything. Let’s say in my case I can name it something like nixie-mainline and git will allow it. But as per the standard master is set as the mainline branch.

Git Main Branch

The main reason for switching the default branch name from master to main is that the term master is considered offensive by many people. In the computing world, the term master and slave is used to refer to the systems where the core process and the agent process are running.

The software freedom conservancy and the git maintainers decided to introduce an option with git where the user can decide the default branch name.

The word main is considered a good alternative to the word master and companies like Github, Atlassian, GitLab, etc started making changes to their systems to set the default branch name to main.

Set Default Branch Name In Git Using init.defaultBranch Configuration

The Git version 2.28.0 introduces a configuration called initi.defaultBranch that lets you set the default branch name.

Run the below git config command which will add the default branch name to the .gitconfig file in your home directory.

$ git config --global init.defaultBranch <BRANCHNAME>
$ git config --global init.defaultBranch main

If you wish to set the default branch at the system level use --system instead of --global. This will add the configuration to the /etc/gitconfig file.

$ git config --system init.defaultBranch main

Alternatively, you can also directly open the ~/.gitignore or /etc/gitignore file and add the following property.

[init]
    defaultBranch = main

Now create a new git repository and you will see main as the default branch.

Set Default Branch Name Through Git Bash Installation

In Windows, when installing git bash it will prompt you to choose the default branch name.

Git bash Set Default Branch Name
GitBash Set Default Branch Name
  • If you choose “Let Git decide” it will use master as the default branch name.
  • If you choose the “Overrride” option then you can provide the custom default branch name. By default, the main will be picked as shown in the above image.

Set Default Branch Name In Github

By default, the github branch is set to main. If you create a new repository it will show you instructions to rename the master branch to main before pushing the changes to github.

Github Rename branch
Github Rename Branch To Main

Note: If your local repo branch name is already set to main you can ignore the above highlighted step.

You can set the default branch name for repositories by going to “SETTINGS -> REPOSITORIES -> DEFAULT BRANCH NAME”

Github default branch
Github Default branch

Wrap Up

In this guide, we have seen the reason behind switching from master to main and different ways to set the default branch name.

Leave a Reply

9 − 3 =