How to rename a Git branch name locally and remotely

Learn how to easily rename a Git branch name using simple steps. Update your branch names with confidence and maintain a clean and organized repository. Get started now!

Updated: 04 Jul, 23 by Susith Nonis 6 Min

List of content you will read in this article:

It takes a lot of labor to create a website or piece of software; one team of developers will work on a new feature, another will test it to see whether it functions as intended, another may be addressing bugs, and so on. 

Managing all these versions of the same code base must be a little challenging. This is where Git comes to play. Git is a DevOps tool for managing source code. It is a version control system that can efficiently manage small to large projects and is free and open-source. 

Git is a tool used to log source code changes, allowing several engineers to collaborate on non-linear development. Git was developed in 2005 by Linus Torvalds for the Linux kernel development and is by far the most popular version control system in use today.

This tool's unique feature is that many developers can work on a project simultaneously and independently. Using this tool, each user can download a copy to his or her local computer, as Git doesn't operate with a centralized repository. This serves as a backup as well. 

It is crucial to segregate work phases and keep an overview when many developers contribute to a project and take divergent methods. Git makes use of "branches" for this. 

Git Branches lets us work on various iterations of our code at once. Consider branches as different timelines. Git Branches allow us to set up distinct environments where we may test new ideas or even work on several projects simultaneously without endangering the codebase.

Until we combine or integrate the changes, changes made on one branch do not affect the other.

Branching is a regular element of the development process in Git. Essentially, git branches are a pointer to a snapshot of your modifications. No matter how big or tiny the bug is, if you want to add or fix a new feature, you create a new branch to contain your changes.

What occurs if a branch is wrongly named? Is it necessary to delete it? No. Git may occasionally be inconvenient, but it does have a technique for renaming branches.

The following will explain how you can rename a Git branch locally and remotely.

You've established a naming scheme for git branches and are working on a project with several developers. After creating a new branch and pushing the modifications to the remote repository, you discovered that the branch name was unintentionally misspelt.

Fortunately, Git makes renaming the branch quite simple. Use the -m command to accomplish this. The corresponding syntax always has the following format:

“git branch -m <old-name> <new-name>”.

The branch you want to rename is called "old," and its new name is called "new." However, there are distinctions between already remote branches and those that you change locally. We outline the procedures for both scenarios below.

Renaming a local Git branch

A local Git branch is confined to your machine. You do tests and modifications here without the knowledge of the other developers. It can therefore be quickly renamed.

  • Choose the Git branch you would like to rename in the command line. Here, you should use the “git checkout old-name” command. 
  • You'll receive confirmation that you have chosen the right branch. The command line is “Switched to branch 'old-name'”.
  • Execute the actual renaming of the local Git branch at this point.  “git branch -m new-name” is the right command.

As an alternative, you can rename the Git branch using the master. Use the following actions to accomplish this:

  • Use the command "git checkout master" to change to the master.
  • Type the following command: "git branch -m old-name new-name".
  • Use the "git branch -a" command to get the branch's current status to verify that the rename was successful.

⭐⭐Unlock unlimited possibilities for your online presence with our powerful and reliable VPS hosting solutions, empowering your website with lightning-fast speed, enhanced security, and unparalleled flexibility.⭐⭐

Renaming a remote Git branch

You can't just rename a Git branch in a remote repository because it would cause issues. Instead, you must remove the previous name before adding the new branch. 

Fortunately, this is not too difficult and can be accomplished with a few straightforward commands. Similar to the local branch, users have two choices.

  • First, confirm that the local branch has the new name, which is correct. "git branch -a" is the right command to use.
  • Remove the branch with the outdated, inaccurate name from the remote repository using the “git push origin --delete old-name” command. 
  • Check to see if the old branch was correctly deleted.
  • Add the branch with the right name at this point. Use the "git push origin -u new-name" command.
  • Finally, reset the upstream branch to make sure the modifications took effect.

However, you can also use the following command to rename the remote Git branch.

  • Use the “git push origin:old-name new-name” command.
  • Additionally, carry out the reset above of the upstream branch.

Branches, one of Git's most useful features, are a component of the software development process. In essence, branches are references to specific commits.

Renaming a git branch is occasionally necessary while using git. There are various reasons why the branch name might need to be changed, but frequently this occurs because the scope of the code modification has changed. 

As a result, the git branch name should be adjusted to reflect the branch's purpose better. It’s very simple to rename a Git branch. With only a few commands, it may be completed rapidly. 

For local branches, the procedure is a real rename, but for the remote branch, a deletion is required. After rectifying the mistake, it makes sense to check again thoroughly. After then, you and other developers can continue working on your project as usual.

People also read: 

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.