How to rename the name of local and remote Git branch?

I have four branches, such as master β†’ origin / regacy, FeatureA β†’ origin / FeatureA. As you can see, I typed the wrong name.

So I want to rename the name of the remote branch (origin / regacy β†’ origin / legacy or origin / master)

I try the command below:

git remote rename regacy legacy 

But the 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?

+311
git git-branch repository rename
Jun 02 '15 at 7:36
source share
7 answers

Enter image description here




You cannot directly rename a remote branch. You must delete it and then click it again.

Rename a branch

 # 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 # 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 

Enter image description here




Important Note:

When you use git branch -m (move), Git also updates your tracking branch with a new name.

git remote rename legacy legacy

git remote rename trying to update the remote section in your configuration file. It will rename the remote with the given name to a new one, 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 remote local configuration name, not the remote branch.




Note. Git servers can allow you to rename Git branches using the web interface or external programs (such as Sourcetree, etc.), but you must remember that in Git all work is done locally, so it is recommended to use the above commands. to work.

+580
Jun 02 '15 at 7:44
source share

If you incorrectly spelled a branch and pushed this button to a remote repository, follow these steps to rename this branch ( based on this article ):

  1. Rename your local branch:

    • If you are in a branch, you want to rename:
      git branch -m new-name

    • If you are in another branch:
      git branch -m old-name new-name

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

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

+84
Aug 08 '17 at 7:26
source share

There seems to be a direct way:

If you really want to rename branches remotely (without renaming any local branches at the same time), you can do this with a single command, for example

git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

Renaming branches remotely in Git

See the original answer for more details.

+28
Feb 11 '17 at 7:43 on
source share

This can also be done as follows.

Rename the local branch first, then the remote branch.

Renaming a local branch:

If logged in to another branch,

 git branch -m old_branch new_branch 

If you are logged into the same branch,

 git branch -m new_branch 

Rename a remote branch:

 git push origin :old_branch // Delete the remote branch git push --set-upstream origin new_branch // Create a new remote branch 
+16
Oct. 14 '18 at 8:19
source share

There is no direct method,

  1. Rename your local branch ,

    My current master branch

    git branch -m master_renamed #master_renamed - new name of the master

  2. Delete remote branch,

    git push origin --delete master #origin is remote_name

  3. Click the renamed branch to the remote,

    git push origin master_renamed

It...

+1
May 11 '18 at 1:41
source share

This can be done even without renaming the local branch in three simple steps:

  1. Go to your repository on GitHub
  2. Create a new branch from the old branch you want to rename
  3. Delete old branch
+1
Feb 18 '19 at 10:40
source share

I use this alias git and it pretty much does the job automatically:

 git config --global alias.move '!git checkout master; git branch -m $1 $2; git status; git push --delete origin $1; git status; git push -u origin $2; git branch -a; exit;' 

Use: git move FROM_BRANCH TO_BRANCH

This works if you have default names like master, origin, etc. You can change as you want, but it gives you an idea.

0
Jul 24 '19 at 9:56
source share



All Articles