Git Basics: How to rename a Git Branch?


These days, Git is one of the most popular version control systems available for software development. Git was created by Linus Torvalds in ... - Article authored by Kunal Chowdhury on .

These days, Git is one of the most popular version control systems available for software development. Git was created by Linus Torvalds in 2005 for development of the Linux kernel and gradually used as a free software distributed under GNU General Public License version 2.

 

Though there are many UI tools available for Git, but sometimes working with Git command is very tricky. Today in this post, we will learn the basics on renaming a Git branch.

 

How to rename a Git branch (www.kunal-chowdhury.com)

 

It’s a general issue for the developers working in Git to rename an existing branch that they are currently working on. Let me give a simple use case to demonstrate the problem.

 

Suppose, you are working on a branch but later you decided to change the name of it to identify easily. As it is not like the style of Windows Explorer renaming, you need to provide few commands to rename the branch and push the changes to the remote repository.

 

First, you need to provide the command to rename:

git branch –m [Old_Name] [New_Name]   

 

Then you need to delete the previous branch and commit the new one:

git push origin :[Old_Name] [New_Name]

 

 

There are few additional commands, which you can use too:

git branch –m [New_Name]                             # When you provide the command inside the [Old_Branch]

git branch –m [Old_Name] [New_Name]        # When the command provided from root

git branch –M [Old_Name] [New_Name]        # Force to rename the branch, even though it exists

 

git push origin :[Old_Name]                             # Delete the old branch from remote

git push origin [New_Name]                            # Push the new branch to remote

git push origin :[Old_Name] [New_Name]      # Delete the old branch and push the new branch to remote

 

git push --set-upstream origin [New_Name]  # Push the new branch, set local branch to track the remote

 

 

Was this post useful? If you came to this page by searching online about renaming git branch, please have a look into my other blog posts. Subscribe to the RSS feed or the newsletter to keep yourself updated. If you have any queries, you can either ping me on Twitter or drop a line below in the comments section.

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.