We’ve all been there. You create a feature branch with a name like fix-bug-123, only to realize ten minutes later that the bug is actually a feature request, or worse, you simply made a typo. When I first started with version control, I thought I had to delete the branch and recreate it, which is a nightmare if you’ve already pushed commits.

Knowing how to rename git branch locally and remotely is a fundamental skill that keeps your repository clean and your team’s workflow organized. In this guide, I’ll walk you through the exact process I use in my daily development workflow to rename branches without losing a single commit.

Prerequisites

Before we dive into the commands, ensure you have the following:

Step 1: Rename the Branch Locally

The first part of the process happens entirely on your machine. Depending on whether you are currently on the branch you want to rename or on a different one, the command varies slightly.

Option A: Renaming the current branch

If you have the branch checked out, use the -m (move) flag:

git branch -m new-branch-name

Option B: Renaming a different branch

If you are on main and want to rename old-name to new-name without switching branches:

git branch -m old-branch-name new-branch-name

At this point, your local environment is updated, but the remote server still knows the old name. If you only worked locally, you’re done. But for most of us, the next step is critical.

Step 2: Update the Remote Repository

Git doesn’t have a single “rename remote branch” command. Instead, you have to push the new branch and then delete the old one. As shown in the terminal output in the image below, this is a two-step process.

Push the new branch and reset upstream

Push the locally renamed branch to the remote and set it to track the new name:

git push origin -u new-branch-name

Delete the old branch from the remote

Now, remove the outdated branch name from the server:

git push origin --delete old-branch-name
Terminal output showing the successful push of a renamed branch and deletion of the old remote branch
Terminal output showing the successful push of a renamed branch and deletion of the old remote branch

I’ve found that forgetting the --delete step is the most common cause of “ghost branches” in GitHub, which can confuse your teammates during PR reviews.

Pro Tips for Branch Management

Renaming branches is common, but maintaining a clean Git history is where the real productivity gains happen. Here are a few tips from my experience:

Troubleshooting Common Issues

Sometimes things don’t go as planned. Here are the most frequent hurdles I encounter:

“Branch is currently checked out” error

If you try to delete a branch locally that you are currently on, Git will stop you. Switch to main or develop first:

git checkout main
git branch -d old-branch-name

Merge Conflicts after Renaming

While renaming itself doesn’t cause conflicts, merging the renamed branch into another might. If you run into issues during the final merge, I highly recommend learning how to fix git merge conflicts in VS Code for a visual way to resolve them.

What’s Next?

Now that you know how to rename your branches, you might want to explore more advanced Git workflows. I suggest looking into Git Rebase to keep your commit history linear or exploring Git Hooks to automate branch naming rules for your entire team.

Ready to automate your workflow? Subscribe to the ajmani.dev newsletter for more deep dives into development productivity tools!