Git Rename Branch

Learn via video courses
Topics Covered

In collaborative projects with multiple features, managing branches is crucial. Git facilitates branching, allowing developers to diverge from the main branch, work independently, and avoid disruptions to the primary codebase. Each branch represents a distinct version or line of development, fostering isolated work on specific features or bug fixes. Adopting a naming convention for branches is beneficial, with informative names that start with a category word like "bugfix" or "feature." Best practices include separating words with hyphens, underscores, or slashes, avoiding numeral-only names, and keeping branch names short. Git provides commands to rename both local and remote branches, addressing situations where branch names need correction.

Pre-requisites

Before understanding how we can use git rename branch, we need to understand what exactly are branches in git, and what's the need for having different branches in a repository.

How to Change a Local Branch Name?

There are two different methods in git to change the name of the local branch in a git repository. We will see each of these methods with the help of an example. Consider we are in a testing_folder having 9 branches and we are on the old_branch.

change branch name

Now, suppose we want to rename this old_branch branch to new_branch.

Method 1

The first method to rename a local branch is to use the -m flag along with the git branch command to change the name of the branch. The syntax of the command is:

In the above example, change the name of the branch from old_branch to new_branch. We will use:

change branch name using -m

As you can see in the image above, the branch was successfully renamed to new_branch. With this method, you need to make sure you are on the branch you want to rename before renaming it. Like in this example, we wanted to rename old_branch, so we were at the old_branch branch. This will help the git branch -m <new_branch_name> command to keep track of the old name of the branch you want to rename. If you are already not on the old branch, then you first need to checkout to that branch using git checkout old_branch and then you can run the above command to rename it to new_branch.

Method 2

This method is a slight modification of the above method. Let's say you're on the master branch and now want to rename the testing_branch branch to testing_branch_3. So, as we have seen to apply the above method, we first need to checkout/switch to the branch we want to rename. Then you can rename it using the above git branch -m <new_branch_name> command. However, this second method renames the branch even if you are on a different branch. Doesn't it look nice??? Yes, it is.

verify renaming

You can stay on any branch and can use this slight variation of the above syntax to rename a branch.

In our example, we will stay on the master branch only and we will enter this:

This would rename the testing_branch to testing_branch_3. To check the new branch name, you can run the git branch command which will list all of your branches.

check branch name

As you can check from the above image, the branch name has been changed to testing_branch_3.

These are two methods for renaming local branches in Git. One is to rename a branch using the command git branch -m <new-branch-name> and the other one is used when you want to rename a local Git branch without having the branch checked out using this command git branch -m <branch-name> <new-branch-name>.

How to Rename a Remote Git Branch?

If the branch you are renaming has already been pushed to a remote, then, it isn’t possible to rename a remote branch directly as we did for local branches. Here, you will have to delete the old remote branch name and push a new branch name to the remote repository.

To do that, firstly you will need to git rename a local branch by following the previous methods using the -m option. After doing this, we can push the <new_branch_name> name of the local branch and reset the upstream branch using the command:

This will help in resetting and pushing the upstream branch for your new local branch.

Then, you can simply delete the old name of the remote branch using the command:

That’s it. You have successfully renamed your remote Git branch.

How to Change the Name of the Branch on Github?

To change the name of the branch on Github, you can go to the main page of your repository on GitHub.com. Then, you can click on the Branches button as shown in the image below, it will display the list of branches in a repository.

showing branches in a repo

In the list of branches, you can click the edit option to the right of the branch you want to rename.

edit option to the branch name

It will allow you to type a new name for that specific branch as shown in the image. search a particular branch name

That's it, you can review the environment information and then click on the Rename branch button.

review info and rename branch on github

Conclusion

  • Branching is a concept in git which allows you to diverge from the main branch and continue to work in an isolated manner without messing with the main branch.
  • Each branch is a version of any repository or you can call it the independent line of development, any repository can have as many branches each having a different version of the repository.
  • The action of going from one branch to another branch in a repository is known as branch switching. This action is also referred to as “checkout” in Git.
  • git branch <new_branch_name> command is used to create a new branch in a local repository.
  • git branch command is used to list all the branches in a repository along with the * symbol marked in front of the current branch you are on.
  • There are some of the best practices which you should follow while naming your branch like avoiding long names, starting the branch with category words, or using separators between different words in a branch name.
  • We saw different methods for git rename branch according to the use case.
  • To rename your local branch when you are on the branch you want to rename, then you can use the git branch -m <new_branch_name> command.
  • To rename your local branch if you are on a different branch, then you can use the git branch -m <old_branch_name> <new_branch_name> command.
  • As we have seen, only one command needs to be run to rename a local Git branch. However, you cannot rename remote branches directly. You should first push the renamed local branch and delete the remote branch with the old name.
  • To rename your remote branch you first need to reset the upstream branch for the <new_branch_name> local branch and push it using the command git push origin -u <new_branch_name>. Then, you can delete the <old_branch_name> of the remote branch using the command git push origin --delete <old_branch_name>.
  • To rename the branch on Github, you can directly go to the list of branches in a repository and can edit the name of a particular branch you want to change using the Edit option to the right of the branch name.