How to Rename a Git Branch: Local and Remote

By 

Updated on

5 min read

Renaming a local and remote Git branch with git branch -m

Git does not provide a direct command to rename a remote branch. The standard approach is to rename the local branch, push it under the new name, and delete the old remote branch.

This guide explains how to rename a local and remote Git branch step by step.

Quick Reference

TaskCommand
Rename the current local branchgit branch -m <new_name>
Switch to branch, then renamegit switch <old_name>git branch -m <new_name>
Check which remote branch is trackedgit branch -vv
Push new branch and set upstreamgit push origin -u <new_name>
Delete old remote branchgit push origin --delete <old_name>
Remove stale remote-tracking referencesgit fetch --prune

For a printable quick reference, see the Git cheatsheet .

Rename the Local Branch

If you are already on the branch you want to rename, run:

Terminal
git branch -m <new_name>

If you are on a different branch, switch to the target branch first:

Terminal
git switch <old_name>

Then rename it:

Terminal
git branch -m <new_name>

At this point, the local branch has been renamed. If you have not yet pushed the branch to a remote repository , you are done.

If the branch was already pushed, the rename leaves one thing behind. Git renames the branch but keeps its upstream reference pointing at the old remote name. You can see this with git branch -vv:

Terminal
git branch -vv
output
* feature-auth ad4f1a9 [origin/feature-login] Add login
  main         8302136 [origin/main] Initial commit

The local branch is now feature-auth, but the tracking reference in brackets still reads origin/feature-login. With Git’s default push.default=simple setting, a plain git push refuses to run:

output
fatal: The upstream branch of your current branch does not match
the name of your current branch.

The next section pushes the branch under its new name, which repoints the upstream and clears this error.

Rename the Remote Branch

Renaming a remote branch requires pushing the local branch under the new name and deleting the old remote branch.

  1. Push the <new_name> branch to the remote and set it as the upstream:

    Terminal
    git push origin -u <new_name>
    output
     * [new branch]      feature-auth -> feature-auth
    branch 'feature-auth' set up to track 'origin/feature-auth'.

    The last line confirms that the -u option repointed the local branch at the new remote branch, so git push and git pull work again without extra arguments.

  2. Delete the <old_name> branch from the remote:

    Terminal
    git push origin --delete <old_name>

    An older syntax does the same thing by pushing an empty reference to the branch name:

    Terminal
    git push origin :<old_name>

    Deleting a source branch can close or disrupt open pull or merge requests, depending on the hosting service, so check for open requests before you run this command.

The remote branch is now available under the new name.

Update Collaborators

After renaming a remote branch, team members who had the old branch checked out need to update their local tracking references. They should run the following commands:

Terminal
git fetch --prune
git branch -m <old_name> <new_name>
git branch --set-upstream-to=origin/<new_name> <new_name>

git fetch --prune removes the stale reference to the old remote branch. git branch -m renames the local branch, and git branch --set-upstream-to points the local <new_name> branch at the new remote name.

Troubleshooting

fatal: a branch named '<new_name>' already exists
Another local branch is already using the name you picked. Pick a different name, or overwrite the existing branch with git branch -M <new_name> if you are sure you no longer need it.

fatal: The upstream branch of your current branch does not match the name of your current branch
The local branch was renamed, but it still tracks the old remote branch. Push it under the new name with git push origin -u <new_name> to repoint the upstream.

error: unable to delete '<old_name>': remote ref does not exist
The old remote branch is already gone, usually because a teammate deleted it first. Run git fetch --prune to drop the stale remote-tracking reference.

! [remote rejected] <old_name> (deletion of the current branch prohibited)
You are trying to delete the branch that the remote repository uses as its default, and hosting providers word this rejection slightly differently. Change the default branch in the repository settings first, then delete the old branch.

FAQ

Can I rename the branch I am currently on?
Yes. Running git branch -m <new_name> without switching away first renames the branch you are currently on. You only need to git switch <old_name> first if you are on a different branch.

Do I need to update anything after renaming the remote branch?
Yes, if other people have the old branch checked out. They need to run git fetch --prune, rename their local branch with git branch -m <old_name> <new_name>, and then run git branch --set-upstream-to=origin/<new_name> <new_name> to update their local tracking reference.

What if I only want to rename the branch locally?
Run git branch -m <old_name> <new_name> (or switch to the branch first and run git branch -m <new_name>). As long as you do not push anything, the remote branch is unaffected.

What is the difference between git branch -m and git branch -M?
-m refuses to rename if a branch with the new name already exists. -M forces the rename and overwrites the existing branch. Use -m unless you intentionally want to overwrite.

What if I made a typo in a commit message instead of the branch name?
Use git commit --amend to edit the most recent commit message, or git rebase -i to reword older commits.

Conclusion

To rename a Git branch, rename it locally with git branch -m, push the new name to the remote with git push origin -u <new_name>, and remove the old remote branch with git push origin --delete <old_name>.

For more Git branch operations, see How to Create and List Git Branches and How to Delete a Local and Remote Git Branch . For a quick command overview, see the Git cheatsheet .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page