How-To: Rename the Master Branch

It is well past time for your project to get rid of noninclusive terminology in your git branches! Code is for EVERYONE, so in the interest of making our language serve everyone equally, here is instructions for how to do it.

Starting A New Repo

The process is as follows:

  1. Create to your starting directory.
  2. Initialize the repository.
  3. Create a new “main” branch. I prefer main, trunk or root.
  4. Create a new file in the repo. Start with the .gitignore file.
  5. Add that file.
  6. Commit that change.
$ mkdir demo.branchinit
$ cd demo.branchinit
$ git init
Initialized empty Git repository in {Your Directory}

$ git checkout -b "main"
Switched to a new branch 'main'

$ touch .gitignore
$ git add .

$ git commit -am "First commit."
[main (root-commit) {hash}] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode {something} .gitignore

$ git branch
* main

Voila! The repo is now rooted off of your main branch, and the word master has nothing to do with it.

Changing an Existing Repo

An existing repo also easy to change, even if you have a remote source setup.

The process follows:

  1. Go to your existing master branch.
  2. Create a new ‘main’ branch, from that master branch.
  3. Drop the master branch.
  4. Push your new main branch to your remote.
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'

$ git checkout -b "main"
Switched to a new branch 'main'

$ git branch -d master
Deleted branch master (was {a hash})

$ git push origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'main' on GitHub by visiting:
remote:      https://github.com/myorg/myProject/pull/new/main
remote:
To https://github.com/myorg/myProject
 * [new branch]      main -> main

Now you just need to reset the default branch in your remote.

  1. Navigate to your remote git source. I use my web browser for this.
  2. Update the default branch to your new ‘main’ branch.
    • In GitHub,
      1. Go to your repo, click Settings
      2. Then Branches on the left.
      3. Select main in the Default Branch box, and click Update.
      4. Then click through the confirmation.
    • In Azure DevOps Services
      1. Go to your repo, then Braches.
      2. Locate the main branch, and then click the 3 dots at the end of the row.
      3. Click ‘Set as default.’

Finally, clean up your local repo’s remote branches.

  1. While still in your main branch, reset the head on your remote.
  2. Delete the master branch!
$ git remote set-head origin -a
origin/HEAD set to main

$ git branch -d master
Deleted branch master (was {a hash})

Alright, you still have some work to do, but your local repo is nice and clean. You’ll still have to clean up your remote, but you can do so just like you would with any other branch delete.

Thanks for doing your part to make everything more inclusive! #blacklivesmatter

One thought on “How-To: Rename the Master Branch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s