Renaming branches remotely in Git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git does not have a single remote "rename branch" command. In practice, renaming a branch remotely means renaming the branch locally, pushing the new name to the remote, and then deleting the old remote branch name.
That sounds simple, but there are a few places where people get tripped up: upstream tracking, default-branch settings, open pull requests, and teammates who still have the old branch checked out locally.
Rename the Local Branch First
If you are currently on the branch you want to rename, use:
If you are renaming a different local branch, specify both names:
The -m flag moves the branch name to the new one. At this point the rename exists only in your local repository.
Push the New Branch Name to the Remote
Next, publish the renamed branch and set its upstream tracking branch:
The -u option matters because it updates the local branch so future git push and git pull commands know which remote branch to use.
If you skip this step, the branch rename appears to work locally, but your default push and pull behavior may still be tied to the old name or may not be configured at all.
Delete the Old Remote Branch Name
Once the new remote branch exists, delete the old one:
That removes the stale remote reference. After this point, the remote repository effectively treats the new branch name as the canonical one.
The full sequence usually looks like this:
Special Case: Renaming the Default Branch
If the branch you are renaming is the default branch on GitHub, GitLab, or another hosting platform, there is usually one extra step in the web UI: update the repository's default branch setting.
Most hosting platforms also help with redirects and open pull requests, but you should still verify:
- the default branch setting,
- branch protection rules,
- CI workflows keyed to the old branch name,
- deployment rules that reference a specific branch.
The Git commands update the repository data. The hosting platform settings may still need manual cleanup.
What Teammates Need to Do
Other developers who already fetched the old branch name need to synchronize their local clone. A common sequence is:
If they do not have a local branch yet, a fresh checkout is simpler:
The important detail is --prune, which removes deleted remote-tracking references such as origin/old-branch-name.
Common Pitfalls
- Renaming only the local branch and forgetting to push the new name.
- Pushing the new branch but forgetting to delete the old remote branch, which leaves both names active.
- Forgetting to update the upstream branch after the rename.
- Renaming the default branch without checking branch protection and CI settings on the hosting platform.
- Leaving teammates on stale remote-tracking references because nobody pruned the deleted branch.
Summary
- Remote branch rename is really a push-new and delete-old workflow.
- Rename locally with
git branch -m, then push withgit push -u. - Delete the old remote name with
git push origin --delete. - Check hosting-platform settings if the branch is protected or is the default branch.
- Tell teammates to
fetch --pruneso their local state matches the remote.
Related reading
- Replace remote tag with Git
- Repository is not signed in docker build
- Repository not necessary when implementing JpaRepository?
- Reset all changes after last commit in git
- Reset local repository branch to be just like remote repository HEAD
- Resetting remote to a certain commit
- Resetting remote to a certain commit
- Resolve conflicts using remote changes when pulling from Git remote
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.