2 minutes April 30, 2024

How to Rename a Branch Both Locally and Remotely in Git

Renaming branches in Git is a common task, whether you’re working on your own or collaborating with a team. This is what this article explores how to rename a branch both locally and remotely in Git. Before doing so, you need to ensure you have installed Git on your system and that you know the basics of the Git commands.

Steps to Rename a Branch

Step 1. Rename the Local Branch

Open your terminal or command prompt and navigate to your Git repository directory. 

Rename the branch locally using the command:

bash

git branch -m old_name new_name

Replace old_name with the current name of the branch you want to rename, and new_name with the desired new name for the branch.

Step 2. Delete the Old Branch on Remote

Next, delete the old branch from the remote repository using the following command:

 

bash

git push origin --delete old_name

This command removes the old branch named old_name from the remote repository.

Step 3. Update the New Branch’s Upstream

To prevent Git from using the old name when pushing changes to the remote, unset the upstream branch for the new branch:

 

bash

git branch --unset-upstream new_name

This command removes the upstream tracking configuration for the new_name branch.

Step 4. Push the Renamed Branch to Remote

Now, push the renamed branch to the remote repository using the following command:

 

bash

git push origin new_name

This command pushes the new_name branch to the remote repository.

Step 5. Set Upstream for the New Branch

To set the upstream branch for the new_name branch, use the -u flag with the push command:

 

bash

git push origin -u new_name

This command sets the upstream branch for new_name, enabling you to push changes to the branch without specifying the remote and branch name each time.

Conclusion

In case you need to streamline your Git workflow or need expert assistance on the same, then hire a developer who will optimize your processes, get rid of the troubles that may hinder your actions, and ensure a smooth collaboration inside the team.

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Arsha Content writer

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.