Cheatsheet for moving from Master to Main
Tags: tech
Published: 2020-08-21 03:04:51

---
title: Cheatsheet for moving from Master to Main
published: true
description: Notes I have collected on how to renaming the Git default/primary branch
tags: Tech
slug: master_to_main
canonical_url: https://www.swyx.io/writing/master_to_main
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/6wqq892i53i8fn19kcg3.png
---

*[Share on Twitter](https://twitter.com/swyx/status/1296829223051526152?s=20)*

For my own reference, and anyone else interested in moving primary git branch from `master` to `main`. 

> I'm not interested in discussing [reasons to do this](https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx) here, it has been rehashed thousands of times already.

TL;DR: 

![Cheatsheet for Master to Main](https://dev-to-uploads.s3.amazonaws.com/i/6wqq892i53i8fn19kcg3.png)

## Move Existing Projects

### 1. Rename branches

```bash
git branch -m master main # history unchanged
git push origin HEAD
git branch -u origin/main main
```

### 2. retarget existing PRs 

You can use https://github.com/ethomson/retarget_prs to do it.

### 3. set default branch

Make sure you've pushed your `main` branch, then head to `https://github.com/USERNAME/REPONAME/settings/branches` - [docs here](https://docs.github.com/en/github/administering-a-repository/setting-the-default-branch)

### 4. update local clones (if applicable)

This is handy if you are working on local forks of OSS - Thanks to https://twitter.com/xunit/status/1269881005877256192

```bash
$ git branch --unset-upstream
$ git branch -u origin/main
```

## Set Default For New Projects

You can set new projects created on your machine to start with `main` branch as well.

```bash
## Git 2.28+
git config --global init.defaultBranch main
## Git 2.27-
git config --global alias.new '!git init && git symbolic-ref HEAD refs/heads/main'
```

If you're on a Mac like me, you can `brew upgrade git` or [download Git](https://git-scm.com/downloads) to update the version. Recent versions also include [sparse-checkout](https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/), in case you needed more incentive to upgrade.

This is for new projects *on your local machine* - unfortunately GitHub hasn't made a new setting for setting the default `main` branch for new repos created on GitHub yet.

> Edit: now it has! head to https://github.com/settings/repositories to set it

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rs27j4f09wle6cn6h1a3.png)

## Set Bash Aliases

Lastly, you can setup [bash aliases](https://opensource.com/article/19/7/bash-aliases) that tries `main` first and then `master` so you get to use the same alias no matter what you work with:

```bash
alias gpom="git push origin main 2> /dev/null || git push origin master"
```

## GitHub's Plans

If the GitHub process seem rather manual, don't worry. I have hundreds of repos on GitHub. GitHub has plans to release automated tooling to help you manage this.

Plans: https://github.com/github/renaming

- ✅ Jul 17 - redirects of deleted branches
- ✅ Aug 1 - [GitHub Pages work from any branch](https://github.blog/changelog/2020-07-31-build-and-deploy-github-pages-from-any-branch-beta/)
- ✅ Aug 26 - [Be able to change default branch in Github](https://github.blog/changelog/2020-08-26-set-the-default-branch-for-newly-created-repositories/) 
- ✅ Oct 1 - Default branch AUTOMATICALLY changes to `main` on GitHub
- ☑ Summer - Configurable default for new repos
- ☑ EoY 2020 - Retargeting PRs/Releases/Pages for existing repos (https://twitter.com/swyx/status/1351772911082762240?s=20)