1475

I have a local branch master that points to a remote branch origin/regacy (oops, typo!).

How do I rename the remote branch to origin/legacy or origin/master?


I tried:

git remote rename regacy legacy

But this gave an error:

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
JayD
  • 15,483
  • 5
  • 15
  • 14
  • 1
    See http://stackoverflow.com/questions/1526794/rename-master-branch-for-both-local-and-remote-git-repositories – NaN Jun 02 '15 at 07:40
  • 1
    Possible duplicate of [How do I rename a local Git branch?](https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch) – Vineet Jain Aug 26 '17 at 16:06
  • Possible duplicate of [Rename master branch for both local and remote Git repositories](https://stackoverflow.com/questions/1526794/rename-master-branch-for-both-local-and-remote-git-repositories) – Vadzim Oct 01 '19 at 10:22
  • Related question: [Renaming branches remotely in Git](https://stackoverflow.com/q/4753888/3216427) – joanis Nov 17 '21 at 13:31
  • 2
    **In short:** 1. Checkout to the local branch, 2. `git branch -M new-name` 3. `git push -d origin old-name` 4. `git push -u origin new-name`. – aderchox Feb 24 '22 at 07:46
  • 4
    Great answer! I would just re-organise 1. Checkout of branch `old name` 2. Rename `git branch –m old-name new-name` 3. Checkout into new branch `git checkout new name` 4. Push changes to new remote `git push -u origin new-name` 5. Go to the web page create PR in GH, you will see the new branch as well as the old branch 6. Back to branch `new name` you can now delete the origin head of the old branch `git push -d origin old-name` the local and remote now will have only one branch with all the commits in the new branch. The old branch has been safely deleted AFTER new branch created. – Sumi Nov 16 '22 at 13:45
  • Adjacently related: [How can I push a local Git branch to a remote with a different name easily?](https://stackoverflow.com/q/5738797/4561887) – Gabriel Staples Jun 15 '23 at 02:56

20 Answers20

2391

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.

Adam Burley
  • 5,551
  • 4
  • 51
  • 72
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 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'` – Krzysztof Krasoń Jan 26 '18 at 20:47
  • 5
    Is there a way to avoid automatic closure of the open PRs on the old-name branch on the remote? Faced this in gitlab, where the PR on the old branch name was closed after pushing to origin. – Himanshu Tanwar Apr 29 '20 at 09:44
  • 2
    Here is a single-command version of this: `OLD=; NEW=; REMOTE=; git branch -m $OLD $NEW && git push $REMOTE --delete $OLD && git push $REMOTE $NEW && git push $REMOTE -u $NEW` – Irek Mirgaleev Nov 06 '20 at 12:34
  • If the `````` branch you're trying to rename is set as the default remote branch, then you need to change the default remote branch to some other branch else ```git push --delete ``` will fail. – Sanil May 19 '21 at 09:02
  • Hi! Thank you for your answer! What will see other developers when I've done these changes with these 6 commands? What will they have to do on their local machines connected with the old branch name? – giuseppe_para Aug 25 '21 at 15:41
  • 1
    When I run the unset as described in the answer and the comments, I get: "fatal: Branch has no upstream information" – Jim Archer Oct 01 '21 at 15:02
  • 4
    @JimArcher yes because you need to use the new name rather than old name. I changed it but the original author changed it back without explanation. – Adam Burley Oct 13 '21 at 13:43
  • Would this work for a Visual Studio project that's housed remotely in Azure DevOps? Let's say the remote main branch in ADO has been renamed, and I need to rename branch locally to match. – Anne Bailly Jan 21 '22 at 03:18
  • 2
    Maybe you can remove the last but one command `git push ` since it is included in the last one `git push -u `. – TheFabbius Jun 18 '22 at 13:47
620

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

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
ZILONG PAN
  • 6,305
  • 1
  • 11
  • 6
  • 4
    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 – Kevin Hooke Nov 20 '18 at 04:09
  • 3
    @Dr1Ku Need to know the difference between `git push --delete old_name` & `git push origin :old-name new-name` to delete a branch. – Ashutosh Chamoli Dec 03 '18 at 11:49
  • 2
    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` – Colin Oct 04 '19 at 20:04
  • 4
    For Github and Bitbucket users, when you run Step 2, it will create the new remote branch, but you will see an error from the remote about "refusing to delete the current branch". So just go to Repository Settings ➔ Branches ➔ **Change Default / Main Branch** ➔ new_branch_name ➔ Save. Then run Step 2 again, so that it deletes the old remote branch name. – Mr-IDE Jun 30 '20 at 12:53
  • Would this work for a Visual Studio project that's housed remotely in Azure DevOps? Let's say the remote main branch in ADO has been renamed, and I need to rename branch locally to match. – Anne Bailly Jan 21 '22 at 03:21
  • This is the best answer by far. – Felipe Jul 14 '23 at 03:54
157

Check on which branch you are using the command below

git branch -a 

Checkout to the branch you want to rename

git checkout branch_to_rename

Rename the branch using

git branch -m new_name

Push the changes

git push origin :old_name new_name
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ganesh Ghuge
  • 1,721
  • 1
  • 6
  • 7
112
  • Rename your local branch.

If you are on the branch you want to rename:

git branch -m new-name

if you stay on a different branch at the current time:

git branch -m old-name new-name
  • Delete the old-name remote branch and push the new-name local branch.

Stay on the target branch and:

git push origin :old-name new-name
  • Reset the upstream branch for the new-name local branch.

Switch to the target branch and then:

git push origin -u new-name
Vitalii Andrusishyn
  • 3,984
  • 1
  • 25
  • 31
  • 6
    Your solution is simple and clear. Thank you @Vitaliy Andrusishyn for sharing your knowledge. – Niyongabo Eric Aug 01 '20 at 21:21
  • 10
    `git push origin :old-name new-name` closed my open PR. Careful if you have an ongoing discussion in your PR. – Sam Lahm Dec 29 '20 at 21:07
  • 3
    Before entering the `git push origin :old-name new-name` command, make sure your default remote branch is not set to the current branch you want to rename and delete – Ekanem Eno Apr 17 '21 at 14:13
  • almost perfectly simple solution with sam-lahm and ekanem-eno warnings. Thanks guys. – seedme Jan 09 '22 at 00:49
56

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ptim
  • 14,902
  • 10
  • 83
  • 103
  • 1
    Didn't work in git `2.20.1` Deleted the old branch but the new one was not created. – Paul Razvan Berg Feb 19 '19 at 18:20
  • Btw, just in case if you're initialized empty repository and set up origin - you will need extra fetch command before invoking rename. See https://stackoverflow.com/a/13928822/2338477 – TarmoPikaro Sep 16 '22 at 12:41
45

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atequer Rahman
  • 1,171
  • 10
  • 25
26

To rename a local branch

 git branch -m <old_name> <new_name>

Rename your local branch from master to legacy

git branch -m master legacy

To rename a remote branch

  • Delete the remote branch with the old name.
  • push the local branch with the new name to the remote repository.
git push origin :regacy

git push origin legacy
  • git push origin :regacy deletes the remote branch named regacy.
  • git push origin legacy pushes the local branch named legacy to the remote repository and creates a new remote branch named legacy.
loopassembly
  • 2,653
  • 1
  • 15
  • 22
25

If you have already pushed the wrong name to remote, do the following:

  1. Switch to the local branch you want to rename

    git checkout <old_name>

  2. Rename the local branch

    git branch -m <new_name>

  3. Push the <new_name> local branch and reset the upstream branch

    git push origin -u <new_name>

  4. Delete the <old_name> remote branch

    git push origin --delete <old_name>

This was based on this article.

milesmeow
  • 3,688
  • 5
  • 35
  • 58
23

Attaching a Simple Snippet for renaming your current branch (local and on origin):

git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>

Explanation from git docs:

git branch -m or -M option, will be renamed to . If had a corresponding reflog, it is renamed to match , and a reflog entry is created to remember the branch renaming. If exists, -M must be used to force the rename to happen.

The special refspec : (or +: to allow non-fast-forward updates) directs Git to push "matching" branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side.

--set-upstream Set up 's tracking information so is considered 's upstream branch. If no is specified, then it defaults to the current branch.

avivamg
  • 12,197
  • 3
  • 67
  • 61
14

Renaming Git Branch Locally and Remotely:

  1. Start by switching to the local branch which you want to rename:

    git checkout <old_name>

  2. Rename the local branch by typing:

    git branch -m <new_name>

  3. At this point, you have renamed the local branch. If you’ve already pushed the <old_name> branch to the remote repository, perform the next steps to rename the remote branch.

    git push origin -u <new_name>

  4. Delete the <old_name> remote branch:

    git push origin --delete <old_name>

✅ That’s it. You have successfully renamed the local and remote Git branch.

Bayram Binbir
  • 1,531
  • 11
  • 11
6

Another workaround is the following:

  1. Checkout to the branch you want to change
  2. Create a new branch from it
  3. Set upstream to remote
  4. Delete old branch from local and remote

More specifically:

# Checkout to the branch you want to rename
git checkout <old_branch_name>

# Create a new branch from the old one and checkout to it
git checkout -b <new_branch_name>

# Push the new branch to remote
git push -u <origin> HEAD

# Delete local branch
git branch -d <old_branch_name>

# Delete remote branch
git push <origin> -d <old_branch_name>
vchan
  • 835
  • 2
  • 11
  • 27
4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • 1
    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 – ut9081 Sep 17 '19 at 07:47
2

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

  1. Go to your repository in GitHub
  2. Create a new branch from the old branch which you want to rename
  3. Delete the old branch
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vikas
  • 6,868
  • 4
  • 27
  • 41
2

I use these git alias and it pretty much does the job automatic:

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;'

Usage: git move FROM_BRANCH TO_BRANCH

It works if you have the default names like master, origin etc. You can modify as you wish but it gives you the idea.

Tarik
  • 4,270
  • 38
  • 35
2

Along with the other steps others have laid out, remember:

In the event that you are trying to delete the default branch, e.g. master, you will get this error when running git push origin :<branch_name>

! [remote rejected] master (refusing to delete the current branch: refs/heads/<branch_name>) error: failed to push some refs to '<repo_name>'.

a) Change the default before deleting the branch (Github example)

  1. Go to your repo.
  2. Click on "Settings"
  3. Change the default branch as shown in the picture below:

enter image description here

b) Then delete the [target] remote:

$ git push origin :master

Dut A.
  • 1,029
  • 11
  • 22
2

Branches in a Git repository hosted on GitHub can be renamed using the repository settings. As a side effect, branch protection rule(s) in GitHub will be changed, too.

  1. Visit "Branches" in your repository settings: https://github.com/<name>/<repository-name>/settings/branches

  2. Rename branch Branches page in GitHub repository settings

  3. Everyone using this repository has to do locally:

$ git fetch
$ git checkout <new_name>
Markus Schulte
  • 4,171
  • 3
  • 47
  • 58
1

First, make sure the local branch has the correct, new name. The appropriate command is git branch -a.

Now delete the branch with the old, incorrect name from the remote repository. To do this, use the following command git push origin --delete <old-name>

Verify that the old branch has been deleted properly. Now add the branch with the correct name. For this, use the command git push origin -u <new-name>

Lastly, perform a reset of the upstream branch to ensure that the changes are effective.

treckstar
  • 1,956
  • 5
  • 21
  • 26
0

I had to do the following task to rename local and remote branch:

# Rename the local branch to the new name
git branch -m <old_name> <new_name>

#  Delete the old remote branch
git push origin --delete <old_name>

# push to new remote branch - creates new remote branch
git push origin <new_name>

# set new remote branch as default remote branch for local branch
git branch --set-upstream-to=origin/<new_name> <new_name>
Sebastian Viereck
  • 5,455
  • 53
  • 53
0

If you want to use a single command to rename the current branch, like this:

git rename my-new-branch-name

Then, you have to create a file named git-rename, make it executable (chmod +x git-rename) and save it to a folder in your $PATH, containing this:

#!/bin/sh
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
test $# != 1 && cat <<EOF && exit 1
Renames the current branch ($currentBranch) both locally and remotely.
USAGE:
git rename <new branch name>
EOF

newBranch="$1"; shift
git branch -m "$newBranch" && \
git push origin :"$currentBranch" "$newBranch"
Elifarley
  • 1,310
  • 3
  • 16
  • 23
0

Rename remote branch only:

(set -ex; old=oldname; new=newname; git push origin origin/$old:refs/heads/$new :$old)

or:

git-rr() (set -ex; old=$1; new=$2; git push origin origin/$old:refs/heads/$new :$old)

git-rr oldname newname
anton_rh
  • 8,226
  • 7
  • 45
  • 73