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

git rename branch remote
git rename local branch
git rename remote
git delete remote branch
rename local branch git visual studio
git change local name
git change branch name to lowercase
gitlab change local 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?


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>

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


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.

How To Rename a Local and Remote Git Branch, To rename a branch in Git we should use the git branch command followed by -m option. If you are required to change the current branch's name in your project,  In this short article I'll show you how to rename Git branches for both local and remote repositories. Rename a Local Git Branch. To rename a local branch, you'll want to use the branch command like this: $ git branch -m <old-branch-name> <new-branch-name> The -m option is an alias for --move, which is analog to the Unix mv command.

How to Rename Git Local and Remote Branches, Rename a local and remote branch in git. Rename your local branch. If you are on the branch you want to rename: git branch -m new-name. Delete the old-name remote branch and push the new-name local branch. git push origin :old-name new-name. Reset the upstream branch for the new-name local branch. Switch to the 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.

It seems that there is a direct way:

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

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

Renaming branches remotely in Git

See the original answer for more detail.

Rename a local and remote branch in git – Multiple States , git push --set-upstream origin new_branch # Push the new branch, set local my version of git (v2.3.2 on osx) keeps reusing the same remote branch name use answer to "how to rename my branch, both local and remote" I've seen so far. Rename a Remote Git Branch Renaming a remote branch is a bit more involved, and isn’t actually possible in the same way it is for renaming local branches. To do it, you’ll need to rename the local branch, delete the remote branch, and then push the renamed local branch to the remote repo again.

It can also be done the following way.

At first rename local branch, then remote branch.

Renaming the local branch:

If logged in another branch,

git branch -m old_branch new_branch 

If logged in the same branch,

git branch -m new_branch

Renaming remote branch:

git push origin :old_branch    // Delete the remote branch

git push --set-upstream origin new_branch   // Create a new remote branch

rename git branch locally and remotely · GitHub, Luckily in Git you can rename a local branch pretty easily. And while it is also In this short article I'll show you how to rename Git branches for both local and remote repositories. git branch -m <old-branch-name> <new-branch-name>. How do I push a new local branch to a remote Git repository and track it too? -Git push branch asked Jul 3, 2019 in Devops and Agile by humble gumble ( 20k points)

There is no direct method,

  1. Rename Local Branch,

    My current branch is master

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

  2. Delete remote branch,

    git push origin --delete master #origin is remote_name

  3. Push renamed branch into remote,

    git push origin master_renamed

That's it...

Git: Rename a Local and Remote Branch, Then reset the upstream branch for the new-name local branch, and you are done with renaming the remote branch. git push origin -u new-  Now by using the above command, you have changed the branch name in local. So now you have to change this name in the remote. for that, you need to follow three steps. with these three steps you can do git rename remote branch.

Learn How to Rename a Git Branch and More – Git Command Guide, whatever the procedure we are using to rename the git branches, we use the same This command will change the branch name in locally not in remote. it will push your newly renamed branch and all your files to remote. and both remote  Rename a local and remote branch in git If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.

How To Rename Git Remote Branchs and Local Branches , In this guide, learn how to change the name of a Git branch on a local system or remote repository. How to rename a Git branch. Common scenarios renaming Git branches are: Renaming the current branch. Renaming a separate branch other than the current one. Renaming a remote branch. renaming the current branch. Assuming that you are required to change the name of the current branch in your project, then run the following command:

How to Rename a Git Branch (Local and Remote), I have a branch named stage and want to change both its local and remote names. I'm new in git and don't know which command to use. I don't want to rename a remote branch, as described in the Rename master branch for both local and remote Git repositories. How can I rename a local branch which hasn't been pushed to a remote branch? In case you need to rename remote branch as well: How do I rename both a Git local and remote branch name

Comments
  • See stackoverflow.com/questions/1526794/…
  • Possible duplicate of How do I rename a local Git branch?
  • Possible duplicate of Rename master branch for both local and remote Git repositories
  • You still need to do something, after the above and when I try to pull I get message: Your configuration specifies to merge with the ref ''refs/heads/old_name'
  • Dont forget to unset the old upstream: git checkout <new_name> ; git branch --unset-upstream
  • @MiguelPing's comment is important. When trying to rename a branch I've already pushed to github, deleting, renaming local, it and re-pushing results in the old name being used again. If I --unset-upstream before re-pushing, it works as intended.
  • After the first 2 steps, if you get an error message about the current branch points to a non-existence branch on the remote repo, the 3rd step fixes this
  • @Dr1Ku Need to know the difference between git push <remote> --delete old_name & git push origin :old-name new-name to delete a branch.
  • BitBucket users: fix error on Step 2 if renaming master, in Repo Details set the default branch to new branch. The error is: By default, deleting the current branch is denied, because the next 'git clone' won't result in any file checked out, causing confusion. You can set 'receive.denyDeleteCurrent' configuration variable to 'warn' or 'ignore' in the remote repository to allow deleting the current branch, with or without a warning message. To squelch this message, you can set it to 'refuse'. error: refusing to delete the current branch: refs/heads/master
  • Didn't work in git 2.20.1 Deleted the old branch but the new one was not created.
  • Thanks for your awesome answer.
  • Nice and simple and only 3 steps. Only improvement I can suggest is git push -u origin master_renamed to set the branch as a tracking branch