How to Rename a Git Branch Locally & Remotely - MAGES
How to Rename a Git Branch Locally & Remotely

How to Rename a Git Branch Locally & Remotely

19 May, 2025

Learn the easy way to change a branch name locally and update it on your remote repository (like GitHub or GitLab). Step-by-step instructions.

If you’ve taken a course like Full Stack Web Development with AI, chances are you’ve already worked with Git branches. They’re one of the most useful features in Git. You can create a new branch, try out a feature, and merge it back when it’s ready—all without messing up your main codebase.

But sometimes, you mess up the name. Maybe there’s a typo. Maybe the team changes naming conventions.

Whatever the case, you’ll need to rename your Git branch.

The good news? It’s easy. In this quick guide, I’ll walk you through how to rename both local and remote branches—step by step. You’ll learn the exact commands and when to use them.

Let’s get into it.

Why Would You Even Rename a Branch?

There are several common reasons you might decide to rename a branch:

  1. Typos: It happens to everyone. You type feautre/login instead of feature/login. Renaming fixes the mistake.
  2. Clarity and Consistency: Branch names should be clear. They should tell you what the branch is about. fix-bug-123 is better than temp-branch. If your project adopts a new naming standard (like feat/, fix/, refactor/), you might need to update existing branches.
  3. Scope Change: A branch might start for one small task. Then the scope grows. The original name doesn’t fit anymore.
  4. Temporary vs. Permanent: A branch created for a quick test might turn into something important. It deserves a proper name.
  5. Simply Keeping Things Tidy: A clean repository is easier to navigate. Renaming helps maintain order.

So, renaming is a helpful skill. It keeps your work organized. It makes collaboration easier.

Also read: Choosing the Right Full Stack Developer Course: Duration and Benefits Explained

How to Rename a Local Git Branch

The first step is always to rename the branch in your own local copy of the repository. This is the easiest part. There are two main scenarios:

Scenario 1: You are currently on the branch you want to rename.

This is the most direct way. Git has a simple command for this.

Open your terminal or command prompt. Navigate to your project directory. Make sure you are checked out to the branch you intend to rename. You can check your current branch with git status.

Once you’re on the right branch, run this command:

     git branch -m new-branch-name

Let’s break that down:

  • git branch: This is the command to interact with branches.
  • -m: This is the flag for “move” or “rename”. It tells Git you want to rename a branch.
  • new-branch-name: This is the new, desired name for your branch.

For example, if you are on a branch named typo-bugfix and want to rename it to bugfix-authentication:

     git branch -m bugfix-authentication

Hit Enter. Git performs the rename locally. You won’t usually see a confirmation message unless there was an error. Your current branch will now be bugfix-authentication.

Scenario 2: You are on a different branch (like main or develop).

You don’t always have to switch to the branch you want to rename. You can rename it from another branch, as long as you are not on the branch being renamed.

The command is slightly different. You need to tell Git which branch you want to rename.

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

Here:

  • git branch -m: Again, the rename command.
  • old-branch-name: This is the current name of the branch you want to rename.
  • new-branch-name: This is the desired new name.

For instance, if you are on the main branch, but want to rename feature/login-form-v1 to feature/user-login-v1:

     git branch -m feature/login-form-v1 feature/user-login-v1

Execute the command. Git will find feature/login-form-v1 in your local repository and rename it to feature/user-login-v1. You will remain on the main branch.

To confirm your local rename worked, you can run git branch. You should see the new name in the list. The old name should be gone.

Also read: Full Stack Web Development in 2025: Trends, Tools, and Career Insights

The Remote Challenge: Why Local Isn’t Enough

Okay, you’ve successfully renamed your local branch. Great! But Git is a distributed system. Your local repository is independent of the remote one (like the one on GitHub, GitLab, Bitbucket, etc.).

Renaming your local branch does not automatically rename the corresponding branch on the remote server.

The remote still has a branch with the old name. If you try to push, Git might get confused. It doesn’t know your new local branch new-name is the same as the remote branch old-name. This is a crucial point to understand.

To truly complete the rename process, you need to update the remote repository as well.

How to Rename a Remote Git Branch

Since you can’t directly “rename” a remote branch with a single command like you can locally, the standard procedure involves a few steps:

  1. Delete the old branch on the remote.
  2. Push your new local branch to the remote.
  3. Set your new local branch to track the new remote branch.

Let’s go through each step.

Step 1: Delete the Old Remote Branch

You need to remove the branch that still has the old name on the remote.

     git push origin –delete old-branch-name

Let’s break this down:

  • git push: We are pushing changes to the remote.
  • origin: This is the default name for your remote repository (where you originally cloned from). If your remote has a different name, use that instead.
  • –delete: This is the flag that tells Git you want to delete something on the remote.
  • old-branch-name: Specify the name of the branch on the remote that you want to remove. This is the name before you renamed it locally.

For example, if your old local branch typo-bugfix was tracking origin/typo-bugfix, and you renamed it locally to bugfix-authentication, you’d run:

     git push origin –delete typo-bugfix

This command tells the remote server to remove the branch named typo-bugfix. You should see output confirming the deletion.

Step 2: Push Your New Local Branch to the Remote

Now that the old remote branch is gone, you can push your locally renamed branch. This creates a new branch on the remote with the correct name.

Make sure you are on the newly renamed branch locally (you can check with git status). Then, push it:

     git push origin new-branch-name

Here:

  • git push origin: Pushing to your remote.
  • new-branch-name: Pushing the branch with its new name.

Using our example:

     git push origin bugfix-authentication

This command sends your local bugfix-authentication branch and its commits to the origin remote. A new branch named bugfix-authentication will be created on the server.

You’ll likely see output indicating a new branch was created.

Step 3: Set Your New Local Branch to Track the New Remote Branch

Finally, your local new-branch-name needs to be linked to the remote origin/new-branch-name. This is called setting the upstream branch. It allows you to use simpler commands like git pull and git push without specifying the remote and branch name every time.

While you are on the newly renamed local branch, run this command:

     git branch -u origin/new-branch-name

Alternatively, you can use the longer but equivalent command:

     git branch –set-upstream-to=origin/new-branch-name

  • git branch -u or git branch –set-upstream-to: These flags tell Git to set the upstream tracking branch.
  • origin/new-branch-name: This specifies the remote branch to track. It’s in the format remote_name/branch_name.

Using our example again:

     git branch -u origin/bugfix-authentication

Or:

     git branch –set-upstream-to=origin/bugfix-authentication

Git will confirm that your local branch is now set to track the remote branch.

     Branch ‘bugfix-authentication’ set up to track remote branch ‘bugfix-authentication’ from ‘origin’.

Now, when you are on the bugfix-authentication branch, git pull will pull from origin/bugfix-authentication, and git push will push to origin/bugfix-authentication.

Putting It All Together: The Full Rename Workflow

Here’s the complete sequence of commands to rename a branch both locally and on the remote, assuming you are starting on the branch you want to rename:

  1. Rename locally:
          git branch -m new-branch-name
  2. Delete the old branch on the remote:
          git push origin –delete old-branch-name
  3. Push the new branch to the remote:
          git push origin new-branch-name
  4. Set the new local branch to track the new remote branch:
          git branch -u origin/new-branch-name

If you start on a different branch, step 1 would be git branch -m old-branch-name new-branch-name, and then you would git checkout new-branch-name before proceeding with steps 2, 3, and 4.

What About Your Team?

If you’re working in a team, remember that renaming a branch affects others. Their local repositories still have the old remote tracking branch reference.

After you rename the remote branch (steps 1-3 above), tell your team members. They should update their local repositories. The easiest way for them is typically:

  1. Fetch all changes (including deleted branches):
          git fetch –all –prune

The –prune option is helpful. It removes remote-tracking branches that no longer exist on the remote.

  1. Delete their old local tracking branch (optional but good practice): They might still have origin/old-branch-name visible locally. git fetch –prune removes the remote reference, but their local copy of the old branch might still exist if they ever checked it out. If they did, they might want to delete their local copy:
          git branch -d old-branch-name
  2. Check out the new branch: They can now check out the newly renamed branch:
          git checkout new-branch-name

When they check it out, Git should automatically set up tracking for origin/new-branch-name because it now exists on the remote. If not, they can manually set the upstream like you did in step 4 above: git branch -u origin/new-branch-name.

Communication is key here. Make sure your team knows about the branch rename!

What About Open Pull Requests / Merge Requests?

If you have an open Pull Request (on GitHub, Bitbucket) or Merge Request (on GitLab) associated with the branch you renamed, check the platform’s documentation or UI. Most modern platforms are smart enough to handle branch renames gracefully. The PR/MR might automatically update to point to the new branch name.

However, it’s always best to verify this after completing the remote rename steps. A quick check on the platform’s website confirms everything is still linked correctly.

Verifying Your Work

After going through the steps, take a moment to confirm everything worked as expected.

  1. Check Local Branches: Run git branch. The old name should be gone, the new name should be present, and it should be the currently checked-out branch if you renamed it while on it.
  2. Check Remote Tracking Branches: Run git branch -r. You should see origin/new-branch-name listed, and origin/old-branch-name should be gone.
  3. Check Upstream: Run git status or git branch -vv. Your current branch (new-branch-name) should show that it is tracking origin/new-branch-name.
  4. Check on the Web: Go to your repository’s page on GitHub, GitLab, etc. Look at the list of branches. The old branch name should be gone. The new branch name should appear.

If all checks pass, you’ve successfully renamed your branch locally and remotely!

Troubleshooting Common Issues

  • Permissions: If git push origin –delete or git push origin new-branch-name fails, you might not have the necessary permissions to delete or create branches on the remote. Talk to your repository administrator.
  • Not on the Right Branch: Ensure you are on the correct branch (or specifying the correct old branch name) when running the local rename command.
  • Typos: Double-check your branch names in the commands. Git commands are case-sensitive for branch names.
  • Remote Name: If your remote is not named origin, remember to replace origin with your actual remote name in the push commands. You can check your remote names with git remote -v.

Best Practice: Choose Meaningful Names Early

While renaming is possible, choosing clear and meaningful branch names from the start saves time and effort. Discuss naming conventions with your team. Stick to them. This keeps your repository clean and easy for everyone to understand.

Conclusion

Renaming a Git branch isn’t just changing a label. It involves updating your local repository and coordinating with the remote. While the local part is simple, handling the remote requires deleting the old branch and pushing the new one. Setting the upstream ensures your future pushes and pulls work smoothly.

It might seem like several steps, but each command is straightforward. Now you have the power to correct typos, follow conventions, and keep your branch history clean. This makes your Git workflow more efficient. It helps your team stay organized.

So go ahead. Keep your branches tidy. Rename them when needed. You’ve got the commands now. Happy coding!

SPEAK TO AN ADVISOR

Need guidance or course recommendations? Let us help!

    Mages Whatsup