GIT Branch rename – How to rename a branch in Local & Remote repo

Introduction

Just an Image for "How to rename a branch in Local & remote Repository"

This tutorial will cover “How to rename git branch in Local and in the remote repository”. But why rename the GIT branch?

Suppose we have created a branch and committed our code many times. And we found that there was a mistake while making a branch name.

So this guide will help you rename the branch in Local and on the remote repository.

  1. How to Rename the GIT Branch in Local without checkout the old branch.
  2. How to Rename the GIT Branch in Local with checkout.
  3. Rename the GIT Branch in Remote Repository.

GIT is the Version control system used to manage the source code of an application. Where many developers can work on the same source code with the Sync.

We can create the GIT repositories for the Application and keep the repo’s on GitLabGitHubBit bucket, and many more. Want to install GIT on Ubuntu refer to the link.


How to Rename the GIT branch in Local without Checkout the old branch.

So without checkout means we will not go to that branch and change the branch name with the “git branch -m” command.

$ git branch -a

OUTPUT:
* main
  old_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/old_branch

By using “git branch -a” we can find the branches that are in the local and remote repositories.

$ git branch -m old_branch new_branch

So we have renamed the branch name now let’s verify by using “git branch -a”.

$ git branch -a

OUTPUT:
* main
  new_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/old_branch

How to Rename the GIT branch in Local with Checkout the old branch.

So checkout means, we will go to that branch and then rename the branch. Let’s check the branches that are present in the git.

$ git branch -a

OUTPUT:
* main
 old_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/old_branch

So currently we are at the main branch, now we will switch branch with the command “git checkout”.

$ git checkout old_branch
$ git branch -m new_branch

So with the command “git branch -m new_branch” we have changed the branch name.

$ git branch -a

OUTPUT:
  main
* new_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/old_branch

How to Rename the GIT Branch in Remote Repository.

So to rename the branch in the remote repository we have to rename it at our local. So here we are assuming with the above steps you had renamed the branch in local.

$ git branch -a

OUTPUT:
  main
* new_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/old_branch

Then push the new branch with the origin into the remote repository. It will ask for a username and password to push the branch into the remote repository.

$ git push origin -u new_branch

OUTPUT:
Username for 'https://gitlab.com': amashmishra9
Password for 'https://[email protected]': 
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for new_branch, visit:
remote:   https://gitlab.com/amashmishra9/tastethelinux-Group/-/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch
remote: 
To https://gitlab.com/amashmishra9/tastethelinux-Group.git
 * [new branch]      new_branch -> new_branch
Branch 'new_branch' set up to track remote branch 'new_branch' from 'origin'.

Once the branch gets pushed you can verify with the “git branch -a”.

$ git branch -a 

OUTPUT:
  main
* new_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/new_branch
  remotes/origin/old_branch

But you can find the old branch on the remote repository, use the -d option with git to delete the branch. If you want to learn How to delete the branch in GIT follow the link.

$ git push origin -d old_branch

OUTPUT:
Username for 'https://gitlab.com': amashmishra9
Password for 'https://[email protected]': 
To https://gitlab.com/amashmishra9/tastethelinux-Group.git
 - [deleted]         old_branch

After the old branch gets deleted you can verify with the “git branch -a” command.

$ git branch -a

OUTPUT:
  main
* new_branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/development-branch
  remotes/origin/main
  remotes/origin/new_branch

Conclusion

In this guide, we learn How to rename the branch into local with 2 methods. First, we look at how to do without checkout the branch, and then with the doing checkout the branch.

Then we saw How to rename the branch in the remote repository. In that, we rename the branch in local and then push the new branch to the remote repository. After the new branch gets pushed we deleted the old branch. Any issues or feedback let us know in the comment section.


Give your valuable time