How to Rename a Git Branch (Local and Remote)

To rename the branch you are on, run git branch -m new-name. For another branch, run git branch -m old-name new-name. Git cannot rename a branch on a remote, so push the new name with git push -u origin new-name, then delete the old one with git push origin --delete old-name.

Rename commands by situation
SituationCommand
Rename the current branchgit branch -m new-name
Rename a different branchgit branch -m old-name new-name
Overwrite an existing namegit branch -M old-name new-name
Publish the new name and track itgit push -u origin new-name
Remove the old name from the remotegit push origin --delete old-name
Fix a clone after someone else renamed itgit branch -u origin/new-name new-name

Updated

Renaming locally is one command; the remote is where people get stuck, because a remote branch cannot be renamed in place, only replaced. Fill in the old and new names below to get the commands for your case, including what teammates must run afterwards and the extra steps when the branch is the GitHub default.

Full rename, start to finish

Rename a pushed branch everywhere

Renames locally, publishes the new name with tracking, then removes the old remote branch.

git switch feature/old-name
git branch -m feature/new-name
git push -u origin feature/new-name
git push origin --delete feature/old-name

What teammates run after a rename on GitHub

These are the steps GitHub lists for updating an existing clone, with the optional cleanup of stale remote branches.

git branch -m old-name new-name
git fetch origin
git branch -u origin/new-name new-name
git remote set-head origin -a
git remote prune origin

What GitHub updates when you rename there

GitHub redirects web URLs containing the old name, moves branch protection to the new name, and retargets open pull requests and draft releases based on it. Raw file URLs are not redirected.

Writers can rename ordinary branches; renaming the default branch or a protected branch needs admin rights, and organisation rulesets can tighten that further.

Avoiding names that break later

Stick to lowercase letters, digits, hyphens and slashes for grouping, such as fix/login-timeout. Git rejects names with spaces, two dots in a row, a trailing dot or slash, a leading dash, or any of the characters ~ ^ : ? * [ and backslash.

How it works

  1. Enter the old name, the new name and the remote, usually origin.
  2. Tick whether you are on that branch and whether it already exists on the remote.
  3. Run the commands in order; the builder adds the push, the delete and the upstream fix only when they apply.
  4. Send the teammate block to anyone else with a clone.

Tips

Frequently asked questions

Can I rename a remote branch directly?

Not with plain git. You push the branch under the new name and delete the old name. GitHub's web interface can rename a branch on the server, which also keeps pull requests and links working.

What is the difference between -m and -M?

-m refuses if a branch with the new name already exists. -M is short for --move --force and replaces it. Neither will rename a branch that is checked out in another worktree of the same repository.

How do I rename master to main on GitHub?

In the repository on GitHub, open Branches, choose rename on master, and type main. Renaming the default branch needs admin access. Then everyone runs the four commands GitHub shows: rename locally, fetch, set the upstream, and update origin's HEAD.

Why does git push still go to the old branch name?

The renamed branch still tracks the old upstream. Push with -u once, or run git branch -u origin/new-name, and later pushes and pulls use the new name.

Related: Git cheat sheet, Git rebase guide, Git worktree guide, GitHub personal access tokens.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap