How to change git default branch from master
Tags: git, beginners, tutorial, productivity
Published: 2020-08-25 13:01:23

---
title: How to change git default branch from master
published: true
tags: git, beginners, tutorial, productivity
canonical_url: https://gitbetter.substack.com/p/how-to-change-git-default-branch
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5e782e0-90e4-41bd-a8c2-ad003d9ebf25_940x788.png)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). 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](https://git.github.io/rev_news/2020/07/29/edition-65/) 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.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1a13be50-c3cd-4bc7-927b-d27e754fa7a1_473x217.png)

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.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddf2d63-43e7-45f8-80c3-7db5a74c7e79_673x429.png)

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.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F576656a1-1c1f-4231-a724-43a940bbeb20_1256x568.png)

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
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F18b478fe-e145-4a47-a108-0ddc90063d6c_627x331.png)

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.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F87b808b2-d242-4ae5-9339-96a19e8a9e94_562x348.png)

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](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.