Change Git Branch Master to Main Locally and Remotely |Video upload date:  · Duration: PT1M0S  · Language: EN

Quick guide to rename a Git branch from master to main on local repo and on remote including commands cleanup and tips

What we are doing and why it matters

We are going to rename the primary Git branch from master to main so your CI does not throw a tantrum and new contributors do not accidentally create more masters. This is a practical git branch rename tutorial that covers local changes remote updates and how to keep other clones and CI happy.

Rename the local branch

If you are sitting on the master branch and want to keep your working tree intact run this single command. It moves the branch pointer and keeps your files where they are.

git branch -m master main

If you were on another branch you can rename a specific branch from elsewhere by running git branch -m oldname newname while not checked out on the branch to be renamed.

Push the new main and set upstream

Send the new branch to the remote and tell your local repo to track it. This makes future push and pull calls behave normally.

git push -u origin main

Remove the stale remote master

Now remove the old remote branch so the project does not keep an obsolete master that confuses contributors and tooling.

git push origin --delete master

Update remote HEAD and hosting default branch

Tell the remote to update its symbolic default branch reference and then change the default branch setting on your hosting provider like GitHub or GitLab through the web UI. Doing both prevents surprises when opening the repo in a browser.

git remote set-head origin -a

Bring other clones and CI up to speed

Each contributor and each CI runner needs a tiny refresh or they might accidentally recreate master or keep deploying from the wrong branch. Share these commands with your team.

  • Fetch new refs from origin with git fetch origin
  • Set the upstream branch locally with git branch -u origin/main main
  • Delete local master if you no longer need it with git branch -d master or use -D if you are stubborn and sure about losing local commits

Search and fix hard coded references

CI scripts deploy configs automation and docs may mention master by name. Grep the repo and update anything that would break when the default branch is main.

git grep -n "master" || true

Replace occurrences in deployment scripts CI yaml files and documentation. Also update any webhooks or service settings that target master explicitly.

Quick checklist before you call it done

  • Rename local branch
  • Push main and set upstream
  • Delete remote master
  • Update remote HEAD and hosting default branch in the web UI
  • Ask teammates to fetch and update their clones
  • Update CI and deployment scripts that referenced master

Done. Your repo now uses main as the primary branch. Tell your team to run a fetch and then either delete their local master or rebase any in flight work onto main. If anyone panics tell them change is part of life and Git is designed to survive it.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.