DEV Community

Srebalaji Thirumalai
Srebalaji Thirumalai

Posted on • Originally published at gitbetter.substack.com on

How to change git default branch from master

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Recently, there are many suggestions about renaming the default branch master to some other name. This was mainly due to the master-slave metaphor that some people are talking about.

There is evidence that states it was intended to mention master-copy or master recording. And it is not intended to master-slave.

But I think it’s people's perspective and if some think that they are not cool with that they can change the default branch.

So let’s see how to change the default git branch _ master _ to _ default _ (you can use your preferred name)

Before starting I have to tell you that I have tried this in multiple repos and its working fine without any breaking changes. If you are not confident enough then fork your repo and try it first.

Prerequisites

  1. Make sure your CI/CD flow doesn’t get interrupted.

  2. Make sure you have merged all the PRs targeting master. Other PRs are fine

There are three steps in renaming the default branch

  1. Change the branch name

  2. Set remote upstream tracking for the new branch

  3. Change the new branch name in repo host (Github, Gitlab)

Change the branch name

git branch -m master default

The above command just renames the default branch to the name default.

-m attribute is used to rename the branch name without affecting the branch’s history.

Now the default branch is changed in the local but not in the remote.

Set remote upstream tracking for the new branch

git push -u origin default

The above command will push the new branch to the remote.

-u attribute is used to set the upstream tracking for the branch.

As you can see that the upstream is set for the new branch. But still, the reference to the old upstream is present in the local.

Change the new branch name in the repo host

In this tutorial, let’s consider Github. But the same option is also available in Gitlab, Bitbucket.

In Github, go to settings -> branches. You can change the default branch there.

That’s it you are done.

But remember that the old branch’s upstream is still present. It won’t affect your workflow. But you should delete it to keep your repo clean.

To delete the old branch’s upstream you can use

git push origin --delete master

As you can see the old remote stream is deleted.


Now the changes are done in your local and in the remote host. Let’s see how to bring those to other people who are already using the repo.

I mean there will be other people who will be using this repo. They have to do few changes to complete the flow.

As you can see still the branch master is present in the local of others who are already using the repo.

There are three steps for people who are already using the repo

  1. Fetch all the branches

  2. Update the upstream remote’s HEAD

  3. Rename the default branch

Fetch all the branches

git fetch

The above command will just fetch all the remote branches to your local.

Update the upstream remote HEAD

git remote set-head origin -a

The above command will query the remote host for the HEAD upstream and it updates that upstream in the local.

Rename the default branch

git branch -m master default

This is the same as the old one. We are just moving the branch without affecting the history of the branch.

As we have already set the remote upstream in the previous step, the new branch is changed and is in sync with the remote.

Now the person can work with the default branch.

As I said earlier, I have tried this is in multiple repos of mine and it’s working fine without any breaking changes. This may seem to be confusing at first, but if you understand the process and read it multiple times you will get familiar.

If you got any doubts or stuck somewhere, you can contact me.

Thank you for reading :)

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Top comments (0)