Skip to content
Log In Sign Up info@muvcloud.com

Rename the Current Branch

git branch -m new-name

-m stands for move. The commits stay exactly as they were; only the name changes. Check the result with git branch, which marks the current branch with an asterisk.

Rename Another Branch

You do not have to switch to a branch to rename it. Name the old branch first, then the new one:

git branch -m old-name new-name

If a branch called new-name already exists, Git refuses. git branch -M old-name new-name forces the rename and replaces that other branch, so its commits are no longer on any branch. Run git log --oneline new-name first if you are not sure what it holds.

Update the Remote

A branch on the remote cannot be renamed with a Git command. Push the new name, then delete the old one:

git push -u origin new-name
git push origin --delete old-name

-u makes origin/new-name the upstream of your local branch, so plain git pull and git push use it from now on. Deleting remote branches is covered in more detail in how to delete a Git branch.

The delete step fails or causes trouble in three cases: the old branch is the default branch of the repository, a branch protection rule covers it, or open pull requests target it. Change those settings on your Git hosting service first. Some services can rename a branch in their web interface and move open pull requests to the new name; if you use that, skip the two push commands and update your clone as described below.

Fix the Upstream

After a local rename, the branch still tracks the old remote name. See what it tracks:

git branch -vv

Once the old remote branch is deleted and you have run git fetch --prune, the line shows the upstream as gone. Point the branch at the new name, or remove the link:

git branch -u origin/new-name
git branch --unset-upstream

Rename master to main

  1. Rename locally and push: git branch -m master main, then git push -u origin main.
  2. On the hosting service, set main as the default branch and move any protection rules to it.
  3. Update deploy scripts, CI settings, and server checkouts that name master.
  4. Delete the old branch: git push origin --delete master.
  5. Refresh the remote's default pointer: git remote set-head origin -a.

To make new repositories start with main, set git config --global init.defaultBranch main, as shown in how to install Git.

What Teammates Run

Everyone with a clone has the old name locally. Each person runs:

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

--prune removes the stale origin/old-name reference. The last line is needed only when the renamed branch was the default branch.

A Checkout on a Server

A deploy checkout that pulls the old name stops getting updates once that branch is deleted. On the server:

cd /var/www/example.com
git status
git fetch origin --prune
git switch -c new-name --track origin/new-name

Check git status first: local edits on the server travel with you when you switch, and a conflicting edit stops the switch. Put them aside with git stash if needed. The old local branch can then be deleted with git branch -d old-name.

Troubleshooting

  • Refusing to delete the current branch: the remote still uses the old name as its default branch. Change the default first. Hosting services may word this rejection differently.
  • Pull says there is no tracking information: the upstream was removed. Set it with git branch -u origin/new-name.
  • The old name comes back: someone pushed it again from an old clone. Ask them to run the teammate steps.

For the commands around this one, see basic Git commands.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations