Git

How To Rename A Remote Branch in Git

admin  

Renaming a remote branch in Git involves a series of steps. This guide will walk you through the process of renaming a remote branch by first handling it locally and then making changes on the remote. Here's a step-by-step walkthrough on how to achieve a git rename remote branch operation.

Here are the step by step instructions to rename a remote branch in git.

Switch to the Branch You Want to Rename:

First, you need to make sure you are on the branch you intend to rename.

git checkout old_branch

You'll see:

Switched to branch 'old_branch'

Rename the Branch Locally:

Once you've switched to your desired branch, rename it with the following command.

git branch -m old_branch new_branch

Upon checking your branches, it should display:

git branch
  master
* new_branch

Remove the Old Branch from the Remote Repository:

Having renamed your branch locally, the next step is to remove the old branch from the remote repository.

git push origin :old_branch

You'll then see a deletion confirmation:

Password for 'https://[email protected]':
To https://[email protected]/user/repository.git
 - [deleted]         old_branch 

Push the Renamed Branch to the Remote Repository:

Lastly, push the renamed branch to the remote repository.

git push origin new_branch

Confirmation will appear as:

Password for 'https://[email protected]':
Counting objects: 1, done.
Writing objects: 100% (1/1), 187 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://[email protected]/user/repository.git
 * [new branch]      new_branch -> new_branch

You've now successfully renamed a remote branch in Git. However, you should keep in mind than renaming branches, especially shared ones, should be done with caution to prevent any issues for other collaborators.

    Git