Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename both a Git local and remote branch name?

I have four branches like master -> origin/regacy, FeatureA -> origin/FeatureA. As you can see, I typed the wrong name.

So I want to rename a remote branch name (origin/regacy → origin/legacy or origin/master)

I try the command below:

git remote rename regacy legacy 

But Git console returned an error message to me.

 error : Could not rename config section 'remote.regacy' to 'remote.legacy' 

How can I solve this problem?

like image 965
JayD Avatar asked Jun 02 '15 07:06

JayD


People also ask

How do I rename a local git branch?

To rename a Git branch, run the following command: git branch -m <old> <new>. This will change the name of the branch you are viewing to the new name you specify. You do not need to specify the old branch name if you want to rename the branch you are viewing.

Can I rename a remote branch in git?

There isn't a way to directly rename a Git branch in a remote repository. You will need to delete the old branch name, then push a branch with the correct name to the remote repository.


2 Answers

schematic, cute git remote graph


There are a few ways to accomplish that:

  1. Change your local branch and then push your changes
  2. Push the branch to remote with the new name while keeping the original name locally

Renaming local and remote

# Rename the local branch to the new name git branch -m <old_name> <new_name>  # Delete the old branch on remote - where <remote> is, for example, origin git push <remote> --delete <old_name>  # Or shorter way to delete remote branch [:] git push <remote> :<old_name>  # Prevent git from using the old name when pushing in the next step. # Otherwise, git will use the old upstream name instead of <new_name>. git branch --unset-upstream <new_name>  # Push the new branch to remote git push <remote> <new_name>  # Reset the upstream branch for the new_name local branch git push <remote> -u <new_name> 

console screenshot


Renaming Only remote branch

Credit: ptim

# In this option, we will push the branch to the remote with the new name # While keeping the local name as is git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name> 

Important note:

When you use the git branch -m (move), Git is also updating your tracking branch with the new name.

git remote rename legacy legacy

git remote rename is trying to update your remote section in your configuration file. It will rename the remote with the given name to the new name, but in your case, it did not find any, so the renaming failed.

But it will not do what you think; it will rename your local configuration remote name and not the remote branch. 


Note Git servers might allow you to rename Git branches using the web interface or external programs (like Sourcetree, etc.), but you have to keep in mind that in Git all the work is done locally, so it's recommended to use the above commands to the work.

like image 75
CodeWizard Avatar answered Oct 09 '22 13:10

CodeWizard


If you have named a branch incorrectly AND pushed this to the remote repository follow these steps to rename that branch (based on this article):

  1. Rename your local branch:

    • If you are on the branch you want to rename:
      git branch -m new-name

    • If you are on a different branch:
      git branch -m old-name new-name

  2. Delete the old-name remote branch and push the new-name local branch:
    git push origin :old-name new-name

  3. Reset the upstream branch for the new-name local branch:
    Switch to the branch and then:
    git push origin -u new-name

like image 37
ZILONG PAN Avatar answered Oct 09 '22 11:10

ZILONG PAN