Rename a Branch in Git

# Introduction

In this tutorial, How to rename a Branch in Git. How to rename both local and remote git branches.

Rename a Local Branch in Git

we can find out the local branches.

$ git branch
$ git branch -a # The -a option lists the remote branches.

Check the local Branch

$ git checkout <old-branch-name>
$ git checkout oldbranch

Rename the Local Branch

we have switch the desired branch. you can rename local branch as command follows

$ git branch -m <new-branch-name>
$ git branch -m newbranch

This command change the name of local branch oldbranch to newbranch

You can also rename a local branch from inside another git branch

$ git branch -m <old-branch-name> <new-branch-name>
$ git branch -m oldbranch  newbranch

Check the New Branch Name

$ git branch -a

How to Rename a Remote Branch in Git

  • You need first to rename the local branch
  • push the new branch to the server
  • delete the old branch from your repository.

Step 1. Rename the Local Branch

$ git branch -m newbranch
# or
$ git branch -m oldbranch newbranch

Step 2: Push the Updated Branch

Push the renamed branch newbranch to the remote server

$ git push origin <new-branch-name>
$ git push origin newbranch

Set the Upstream

Set up tracking between the local branch newbranch and the remote branch newbranch.

$ git push origin -u <new-branch-name>
$ git push origin -u newbranch

Step 3: Remove the Old Branch

$ git push origin --delete <old-branch-name>
$ git push origin --delete oldbranch

Conclusion

You have rename both local and remote git branches. I hope will this your helpful. Thank you for reading the DevopsRoles page!

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.