Open In App

How to Change Branch Name in Git?

Last Updated : 20 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is a powerful version control system widely used for managing code changes in software projects. One common task that developers encounter is renaming branches. Whether due to a change in project scope, a shift in naming conventions, or a simple typo, knowing how to rename branches efficiently is an essential Git skill. This guide will walk you through the steps to rename both local and remote branches in Git.

Why Rename a Branch?

Renaming a branch can be necessary for several reasons:

  • Correcting Typos: Fix any spelling mistakes in the branch name.
  • Adopting Naming Conventions: Ensure branch names adhere to team or project naming standards.
  • Clarifying Purpose: Make the branch name more descriptive and meaningful.

Steps to Rename a Local Branch

Renaming a local branch in Git is straightforward. Follow these steps:

Step 1: Switch to the Branch

Ensure you are on the branch you want to rename. If not, switch to it using

git checkout -b old-branch-name

//Replace old-branch-name with the current name of your branch.

Suppose, In my system their is already a branch with the name 'Geeks', Then I will simply checkout or moved to that.

Screenshot-2024-05-19-150256

Step 2: Rename the Branch

Use the git branch -m command to rename the branch. In this example, I want to change my branch name from 'Geeks to
'GeeksforGeeks'

git branch -m new-branch-name

// Replace new-branch-name with
// the desired new name for the branch.
Screenshot-2024-05-19-150614

Step 3: Verify the Renaming

To confirm the branch has been renamed, list all branches:

git status

You should see the new branch name listed.

Screenshot-2024-05-19-151028

Renaming a Remote Branch

Renaming a remote branch involves a few more steps than renaming a local branch. Follow these steps:

Step 1: Rename the Local Branch

First, rename the local branch as described above.

Step 2: Delete the Old Branch on the Remote

Push the renamed branch to the remote repository and delete the old branch:

git push origin new-branch-name
git push origin --delete old-branch-name

Replace new-branch-name with the new branch name and old-branch-name with the old branch name.

Step 3: Reset the Upstream Branch

Set the upstream branch for the renamed local branch:

git push --set-upstream origin new-branch-name

Handling Branch Renaming in Collaborations

When working in a team, it's important to communicate branch renaming to avoid confusion. Here are a few tips:

  • Notify Team Members: Inform your team about the branch name change through your communication channels.
  • Update Documentation: Ensure any documentation that references the old branch name is updated to reflect the new name.
  • Coordinate Timing: Coordinate the renaming process to minimize disruptions, especially if your team is actively working on the branch.

Conclusion

Renaming branches in Git is a simple yet powerful capability that helps maintain clarity and organization in your version control system. By following the steps outlined in this guide, you can efficiently rename both local and remote branches, ensuring your project stays clean and well-managed. Whether correcting a typo or adopting a new naming convention, mastering branch renaming is an essential skill for any developer using Git.


Next Article
Article Tags :

Similar Reads