In this write-up, we will learn to rename the Git branch. There are different ways to accomplish this task however, we will explore two methods to rename it.
Method 1
Step 1- Git renaming Branch while you are in any branch
git branch -m<oldbranchname> <newbranchName>
here -m is used for the move.
Step 2- Once you move the old branch to the new branch, now you need to push.
git push origin -u <newbranchName>
Step 3- Delete the remote branch
git push origin --delete <oldbranchname>
Method 2: This is another way to rename the branch moving to the branch which needs to be renamed.
Step 1- Switch to a branch that you want to rename
git checkout oldbranchName
Step 2- Rename the local branch
git branch -m newBranchName
Step 3- Push the new local branch to the remote origin
git push origin -u newBranchName
Step 4- Delete the old Branch in remote
git push origin --delete oldbranchName
In this way, we can rename and move the branch to the new one.