How to Rename a Git Local Branch Using the --move Option

Have you ever created a Git local branch and realized that you made a typo in its name or forgot to include the ticket number? Fortunately, Git provides an easy way to rename branches using the --move option. In this article, we'll walk you through how to use this option to rename your Git local branches with some practical examples..

Fix Example 1: Renaming the current branch using --move flag

Say you had created a branch with the name - "fix-for-alignmetn" as there is a typo in it you want to change it to "fix-alignment"

Git Command Example:
# git branch --move fix-for-alignmetn fix-alignment

As you can see we have made use of the "git branch" command followed by the --move flag and then the old branch name followed by the new branch name.

Fix Example 2: Renaming the current branch using -m flag

Git Command Example:
# git branch -m fix-alignment

You can also make use of the shorthand version of the move flag with is -m

Note: If you are renaming the current branch, you do not need to provide the old branch name.

Fix Example 3: Rename a branch that is not current.

Say you are now pointing to the main/master branch and want to rename a different local branch, you can make use of the same syntax as you saw in example 1.

Rename local brach name git

Renaming the branch can help keep your codebase organized and make it easier to track changes.

Be cautious while changing the master branch name

Changing the name of a branch like master/main/mainline/default will break the integrations, services, helper utilities and build/release scripts that your repository uses. Before you do this, make sure you consult with your collaborators. Also, make sure you do a thorough search through your repo and update any references to the old branch name in your code and scripts.

Read -

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap