PRO 30% Free • Stripe ($15) & Razorpay (₹899)

How to Rename a Branch in Git Locally and Remotely

Top Course Staff · 5 min read · Developer tools · PRO · 30% Free Preview Available

How to Rename a Branch in Git Locally and Remotely

Need to rename aGitbranch? You can rename local branches withgit branch -m new-name, but remote branches require a few extra steps. We’ll show you both methods with examples.

At a glance: Branch renaming commands

In this guide, we’ll walk through the complete process for renaming branches in Git locally and remotely, plus best practices to avoid common issues.

What is a branch in Git?

In Git, a branch is a lightweight, movable pointer to acommit. Branches allow us to develop features, fix bugs, or do experiments with new ideas in isolation from the main codebase. By default, every Git repository has amainormasterbranch, but we often create new branches to keep our work organized and collaborative.

Branches are fundamental to workflows like Git Flow,GitHubFlow, and trunk-based development. They enable parallel development, team collaboration, and safe integration. Proper branch naming and management play a critical role in maintaining code quality and clarity across projects.

Now that we understand what Git branches are and why they matter, let’s explore when it makes sense to rename them.

When to rename a branch in Git

There are several scenarios when renaming a Git branch makes sense:

  • Unclear names: Branches liketest123,temp-branch, ormy-stuffdon’t tell us what they do
  • Changed purpose: We started withbugfix-headerbut ended up adding new features too
  • New conventions: Moving fromloginFeaturetofeature/user-loginfor better organization
  • Outdated terms: Replacingmasterwithmainor removing deprecated terminology

With the common scenarios covered, let’s move on to learning how to rename a branch in Git locally.

How to rename a local Git branch

Renaming a local branch in Git is a quick and easy process, but it’s important to understand the commands and implications to avoid issues with version control or remote repositories. Let’s walk through the steps in detail.

Step 1: Identify the current branch

Before we proceed, we need to know whether we’re currently on the branch we want to rename or not. To list all the local branches in the repository and see the current branch, run this command in theterminal:

git branch 
Copy to clipboard
git branch

Here is the output in our case:

* demo   main 
Copy to clipboard
* demo   main

The asterisk (*) symbol beforedemoindicates that we’re currently on thedemobranch.

Step 2: Rename the branch

If we want to rename thedemobranch, we can run this command:

git branch -m fix 
Copy to clipboard
git branch -m fix

Thegit branchcommand with the-mflag allows us to rename a branch. Here, we’ve used this command to rename thedemobranch tofix.

However, we may want to perform this operation while we’re on some other branch. In that case, we can run this command instead:

git branch -m demo fix 
Copy to clipboard
git branch -m demo fix

Step 3: Verify the operation

To ensure the operation is successful, we can run this command again:

git branch 
Copy to clipboard
* fix   main 
Copy to clipboard
* fix   main

As we can see, we’ve successfully renamed thedemobranch tofix.

Next, let’s understand how to rename a remote branch in Git.

How to rename a remote Git branch

Renaming a remote branch in Git takes a few more steps, but it’s manageable. Let’s go through them one by one.

Step 1: Rename the local branch

Let’s assume that a local branchbugfixis tracking the branchbugfixin theremote repositoryorigin. Here, we want to change the name of the remote branchbugfixtofeature.

If we’re on thebugfixbranch, we can run this command:

git branch -m feature 
Copy to clipboard
git branch -m feature

If we’re not on thebugfixbranch, we can run this command instead:

git branch -m bugfix feature 
Copy to clipboard
git branch -m bugfix feature

Step 2: Delete the remote branch

In the next step, we’ll need to remove the branchbugfixfromorigin:

git push origin --delete bugfix 
Copy to clipboard
git push origin --delete bugfix

Step 3: Push the newly renamed branch

Navigate to the newly renamed local branchfeature(if we aren’t already there) andpushit toorigin:

git push origin feature 
Copy to clipboard
git push origin feature

Step 4: Reset the upstream tracking

Link the renamed branch to the newly pushed remote branch:

git push --set-upstream origin feature 
Copy to clipboard
git push --set-upstream origin feature

This ensures proper tracking between our local and remote repositories.

Since we’re done learning how to rename a branch in Git locally and remotely, let’s talk about some best practices that will keep our workflow smooth and organized.

Best practices for renaming Git branches

Apply these best practices to ensure smooth collaboration and avoid disruption:

  • Notify your team before renaming shared branches.
  • Update openpull requestsormergerequests accordingly.
  • Use clear, descriptive names that reflect the branch’s purpose.
  • Avoid frequent renaming to minimize confusion.

By adhering to these best practices, we can maintain a clean and cohesive workflow.

Conclusion

In this tutorial, we covered what Git branches are, why they’re important, and some common scenarios where we should rename them. Then, we learned how to rename Git branches locally and remotely. Finally, we went through some best practices that will help us rename them efficiently.

Git branches empower us to work efficiently and collaboratively. Renaming them — when done thoughtfully — keeps our workflows organized and our codebase professional. By mastering branch management, we can build more maintainable and scalable projects.

If you want to learn more about Git branching and collaboration, check out theLearn Git: Branching and Collaborationcourse on Codecademy.

Frequently asked questions

1. How do I create a new branch locally and push to remote?

Use the following commands:

git checkout -b new-branch-name git push -u origin new-branch-name 
Copy to clipboard
git checkout -b new-branch-name git push -u origin new-branch-name

This creates a new branch locally and pushes it to the remoteoriginwith tracking enabled.

To unlink a local branch from its remote counterpart, remove the upstream configuration while on the branch:

git branch --unset-upstream 
Copy to clipboard
git branch --unset-upstream

You can also edit.git/configmanually, but the above command is safer.

3. Will renaming a branch affect my commit history?

No, renaming a branch does not change the commit history. The commits stay intact; only the branch label is updated.

4. Is it safe to rename themainormasterbranch?

Yes, but with caution. These are default branches in many setups, and renaming them may affect deployment pipelines or integrations. Ensure all team members are informed, and any CI/CD tools are updated.

5. What’s the difference betweengit branch -mandgit branch -M?

  • git branch -mwon’t overwrite an existing branch with the same name.
  • git branch -Mforces the rename even if the target name already exists.

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Production AI Blueprints

Ready to build production-grade AI systems?

Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.