How to Rename a Local and Remote Git branch?

  • May 26, 2023
  • 1
  • 374

How to Rename a Local and Remote Git branch?

Answers (1)
Answer Accepted

To rename a local and remote Git branch, you can follow these steps:

Renaming a local branch:

  1. Open your Git repository in a terminal or command prompt.
  2. Verify the list of branches and make sure you are on a different branch than the one you want to rename. You can use the command git branch to list all the branches and see which one you are currently on.
  3. If you are on the branch you want to rename, switch to a different branch using git checkout <branch-name> or create a new branch to switch to.
  4. Once you are on a different branch, use the command git branch -m <old-branch-name> <new-branch-name> to rename the local branch. Replace <old-branch-name> with the current name of the branch and <new-branch-name> with the desired new name for the branch.

Renaming a remote branch:

  1. After renaming the local branch, you need to push the changes to update the remote branch. Use the command git push origin :<old-branch-name> <new-branch-name> to push the renamed branch to the remote repository. Replace <old-branch-name> with the old name of the branch and <new-branch-name> with the new name of the branch.
    • The colon before <old-branch-name> in the command is essential as it indicates the deletion of the old branch.
    • The <new-branch-name> will create a new branch on the remote repository with the renamed branch's contents.
  2. Verify that the remote branch has been renamed by checking the remote repository, or you can use the command git branch -r to list the remote branches and confirm the changes.

Note: When renaming a branch, both locally and remotely, it may impact other collaborators or branches that are based on the renamed branch. Communication and coordination with other team members are crucial to ensure a smooth transition when renaming branches.

Submit your answer