We’ve all been there. You’re deep in a feature sprint, you create a branch, and then you realize you’ve made a typo or the project requirements shifted, making your branch name completely irrelevant. If you’re wondering how to rename git branch locally and remotely, you’ve come to the right place.
In my experience, renaming a branch is one of those tasks that feels like it should be a single command, but because Git treats remote branches as references rather than editable files, it requires a specific sequence of steps to avoid breaking your teammates’ workflows. In this tutorial, I’ll walk you through the safest way to handle this.
Prerequisites
Before we dive into the commands, ensure you have the following:
- Git installed on your local machine.
- An active repository with at least one branch you wish to rename.
- Push access to the remote repository (GitHub, GitLab, or Bitbucket).
- A basic understanding of the terminal or command prompt.
Step 1: Rename Your Branch Locally
The first part of the process is handling the local reference. There are two scenarios here depending on whether you are currently on the branch you want to rename.
Scenario A: You are already on the branch
If you have the branch checked out, use the -m (move) flag:
git branch -m new-branch-name
Scenario B: You are on a different branch
If you’re on main but want to rename feature-old to feature-new, specify both names:
git branch -m old-branch-name new-branch-name
I usually prefer Scenario B because it allows me to double-check my current state before making changes. If you’ve managed to get into a mess with your local changes before renaming, you might find it helpful to look at my git stash multiple changes guide to clear your working directory first.
Step 2: Update the Remote Repository
This is where most developers get stuck. You cannot technically “rename” a branch on a remote server; instead, you must push the new branch and delete the old one. Here is the workflow I use every time:
1. Push the new branch and set upstream
Push the locally renamed branch to the remote server and establish a tracking relationship:
git push origin -u new-branch-name
2. Delete the old branch from the remote
Now, remove the old branch name from the remote server so your teammates don’t get confused:
git push origin --delete old-branch-name
As shown in the terminal output image below, you will see Git confirming that the old reference was deleted and the new one was created successfully.
Pro Tips for Branch Management
Renaming branches is a common task, but doing it in a team environment requires a bit of etiquette. Here are a few tips from my own production experience:
- Communicate: If other developers are working on the same branch, tell them before you delete the remote branch. Otherwise, they’ll encounter errors during their next
git pull. - Update PRs: If you have an open Pull Request, renaming the branch will usually close the PR. You may need to open a new one or update the base branch in your Git hosting UI.
- Clean up: After renaming, run
git fetch --pruneto clean up stale remote-tracking branches from your local list.
Troubleshooting Common Issues
Error: “branch already exists”
If you get this error, it means you’re trying to rename a branch to a name that is already taken. Use git branch -a to see all local and remote branches and pick a unique name.
Dealing with Merge Conflicts
Sometimes, renaming a branch and then trying to merge it into main can trigger unexpected conflicts if the history has diverged. If you find yourself stuck, check out my guide on how to fix git merge conflicts in VS Code for a visual way to resolve these issues.
What’s Next?
Now that you know how to keep your branch naming clean, you might want to automate your workflow. I recommend looking into Git Aliases. For example, you can create a shortcut for pruning remote branches to keep your workspace tidy.
If you’re managing a large-scale project, consider implementing a Branch Naming Convention (e.g., feature/login-page, bugfix/header-overflow) to avoid the need for frequent renaming in the first place.