git rename branch

Introduction

In this tutorial, How to git rename branch in Git. How to rename both local and remote git branches. Branches are a powerful feature in Git that allows developers to work on multiple features or experiments concurrently.

However, there may be situations where you need to rename a branch for clarity or consistency. In this guide, we’ll walk you through the steps to rename a branch in Git.

How to use git rename branch

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 switched to the desired branch. you can rename the local branch as the command follows

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

This command changes the name of the 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

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 renamed both local and remote git branches. Renaming a branch in Git is a simple process that allows you to maintain clarity and consistency in your project’s branch structure.

Remember, renaming a branch affects only the branch’s name, not its commit history or contents. Other developers working on the branch need to be informed about the name change to ensure smooth collaboration.

Git’s flexibility and branch management capabilities make it a powerful tool for version control and collaborative development. You can use git rename branch.

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 *

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