How to Rename a Branch in Git (Including master to main)
Renaming a branch in Git is a common task — whether you're fixing a typo, adopting a new naming convention, or migrating from "master" to "main". Here's how to do it locally, on the remote, and across your whole team.
Renaming a Local Branch
To rename the branch you currently have checked out:
$ git branch -m <new-name>
To rename a branch you are NOT currently on:
$ git branch -m <old-name> <new-name>
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Tip
Renaming Branches in Tower
In case you are using the Tower Git client, you can rename both local and remote branches simply from the contextual menu — no need to delete and re-push anything:
Renaming a Remote Branch
Remote branches cannot be renamed directly in Git. In practice, you delete the old one and push the new name:
# Delete the old remote branch:
$ git push origin --delete <old-name>
# Push the renamed local branch to the remote:
$ git push -u origin <new-name>
If the renamed branch had an upstream tracking connection, update it:
$ git branch --set-upstream-to=origin/<new-name>
Teammates who had the old branch checked out will need to run git fetch --prune to remove the stale remote reference, then re-establish tracking:
$ git branch -u origin/<new-name> <new-name>
Renaming "master" to "main"
Many teams are moving away from "master" as the default branch name. Here is how to make that change safely.
Step 1 — Rename locally
$ git branch -m master main
Verify the rename worked:
$ git status
On branch main
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Step 2 — Push the new branch to the remote
$ git push -u origin main
Step 3 — Change the default branch on your hosting platform
GitHub (and other platforms) won't let you delete a branch that is set as the default or protected. Change the default branch to "main" through your platform's web interface first:
Step 4 — Delete the old "master" branch on the remote
$ git push origin --delete master
Tip
Renaming "master" to "main" in Tower
In case you are using the Tower Git client, you can rename branches very easily:
After creating the new "main" branch on the remote, you may need to change the default branch or remove any "protected" status for "master" on your hosting platform before you can delete the old branch.
What Your Teammates Have to Do
Collaborators with local clones of the repository need to update their copies:
# Switch to the "master" branch:
$ git checkout master
# Rename it to "main":
$ git branch -m master main
# Get the latest commits (and branches!) from the remote:
$ git fetch
# Remove the existing tracking connection with "origin/master":
$ git branch --unset-upstream
# Create a new tracking connection with the new "origin/main" branch:
$ git branch -u origin/main
Tip
Updating Tracking in Tower
In case you are using the Tower Git client, colleagues can rename their local "master" branch and change the tracking connection from the contextual menu:
Things to Keep in Mind
If you're using CI/CD tools, GitHub Actions, Azure DevOps, Atlassian Bamboo, GitLab CI pipelines, or similar systems, check them carefully after the rename. Any pipeline or webhook that references "origin/master" by name will need to be updated.
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.