Command guide

How to Rename a Git Branch (Local and Remote)

By Admin · Updated · 4 min read · Checked against the official Git docs

To rename the branch you’re on, run git branch -m new-name. That changes the name on your machine only. If the branch is already on GitHub or GitLab, push the new name and delete the old one, because Git has no command that renames a remote branch directly.

Quick answer

git branch -m new-name
git push -u origin new-name
git push origin --delete old-name
Rename a Git branch locally and on the remote with git branch -m

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 18 November 2026, and the output shown is copied from the terminal. Your commit hashes will differ.

Rename a local branch

We made a typo when creating feature/loginn. While on that branch:

$ git branch -m feature/login
$ git branch
* feature/login
  main

To rename a branch you’re not on, give both names, old first:

$ git branch -m old-name new-name
$ git branch
* main
  new-name

-m is short for “move”. If a branch with the new name already exists, Git stops you. -M forces the rename and overwrites that other branch, so check git branch first.

If you only care about your own machine, you’re done. If the branch was pushed, read on, because right now your local branch still tracks the old remote name:

$ git status -sb
## feature/login...origin/feature/loginn

Rename the branch on the remote

Push the branch under its new name, then delete the old one:

$ git push -u origin feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
To /home/sam/remote/shop-site.git
 * [new branch]      feature/login -> feature/login
$ git push origin --delete feature/loginn
To /home/sam/remote/shop-site.git
 - [deleted]         feature/loginn
$ git branch -a
* feature/login
  main
  remotes/origin/feature/login
  remotes/origin/main

The -u flag matters. It points your local branch at origin/feature/login, so plain git push and git pull keep working. Skip it and Git keeps tracking the old, now-deleted branch.

Open pull request on that branch? Deleting the old branch name closes the pull request that used it as its source branch. GitHub’s own docs say the same happens when you rename through the web UI: if the renamed branch is the head branch of an open pull request, that pull request is closed. Merge or close the PR first, or plan to open a new one.

Renaming on GitHub instead

On GitHub you can also rename from the Branches page (pencil icon). Doing it there has two advantages over the command line: links containing the old branch name redirect to the new one, and open pull requests that target the branch are moved to the new name automatically. Renaming the default branch this way requires admin rights on the repository. Everyone’s local clone still has the old name, though, which brings us to the next part.

What teammates need to run

Nobody else’s clone updates by itself. Each person who had the old branch checked out runs:

$ git fetch --prune
From /home/sam/remote/shop-site
 - [deleted]         (none)        -> origin/feature/loginn
 * [new branch]      feature/login -> origin/feature/login
$ git branch -m feature/loginn feature/login
$ git branch -u origin/feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
$ git status -sb
## feature/login...origin/feature/login

git fetch --prune removes the stale origin/feature/loginn and fetches the new name. The rename and -u line up their local branch with it. This is the step most guides skip, and it’s why people end up with a branch tracking something that no longer exists (“upstream is gone” in git status).

Renaming master to main

The default branch is a special case because the remote’s HEAD, branch protection rules and CI settings point at it. The safe order:

  1. Locally: git branch -m master main and git push -u origin main.
  2. On GitHub/GitLab: change the default branch to main in the repository settings, and move any branch protection rules.
  3. Only then delete the old one: git push origin --delete master.
  4. Update anything that names the branch: CI workflow triggers, deploy scripts, badges in the README.

To make main the default for new repos on your machine, run git config --global init.defaultBranch main.

FAQ

How do I rename a local Git branch?

Switch to it and run git branch -m new-name, or from any branch run git branch -m old-name new-name.

Can you rename a remote branch directly?

Not with a single Git command. Push the branch under the new name with git push -u origin new-name, then delete the old one with git push origin --delete old-name. Hosting sites like GitHub also let you rename from their web interface.

Does renaming a branch lose commits?

No. A branch name is just a label pointing at a commit. Renaming moves the label; the commits and history stay exactly the same.

Why does git status say “upstream is gone” after a rename?

Your branch still tracks the old remote name, which was deleted. Fix it with git branch -u origin/new-name.

Written by Admin

Writes practical Git guides for Git Commands. Every command is checked against the official Git documentation.

About Git Commands