<tutorialjinni.com/>

Rename Local and Remote Git Branch

Posted Under: Git, Tutorials on Aug 8, 2023
Rename Local and Remote Git Branch
To rename both a local and a remote branch in Git, you'll need to perform a few steps. Renaming the local branch is similar to what we discussed earlier, but now we'll also rename the corresponding remote branch. Here's how you can do it:

1) Rename the local branch

First, make sure you have the latest changes from the remote repository by running git fetch. Then, rename the branch locally using the following command:
git branch -m old_branch_name new_branch_name
Replace old_branch_name with the current name of the branch, and new_branch_name with the desired new name for the branch.

2) Push the renamed local branch to the remote repository

Push the renamed local branch to the remote repository using the new name:
git push  new_branch_name
Replace with the name of the remote (e.g., origin) and new_branch_name with the new name of the branch.

3) Delete the old remote branch

Now, you need to delete the old branch on the remote repository. You can do this using the following command:
git push  --delete old_branch_name
Replace with the name of the remote (e.g., origin) and old_branch_name with the previous name of the branch. After following these steps, your local and remote branches will be renamed to the new name you provided. Other team members should fetch the latest changes to update their local repositories accordingly. If they were working on the old branch, they can switch to the new branch with:
git checkout new_branch_name
Be cautious when renaming branches, especially in a collaborative environment, to avoid conflicts and miscommunication. It's a good practice to communicate changes with your team to ensure everyone is aware of the updates.


imgae