Reputation: 19243
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'
Upvotes: 1848
Views: 1460978
Reputation: 21
Feel free to use my bash function that renames local and remote branches and resets tracking between them
git-rename-branch() {
if [ $# -ne 2 ]; then
echo "Usage : ${FUNCNAME[0]} <old branch name> <new branch name>"
echo "Example : ${FUNCNAME[0]} master release"
return 1
fi
git checkout $1
git branch -m $2
git push origin :$1 $2
git push origin -u $2
}
It performs the following four steps
In this function the remote is set to "origin" as it is the most often used remote name
Upvotes: 1
Reputation: 9135
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
Upvotes: 0
Reputation: 5877
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>
Upvotes: 1
Reputation: 1420
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"
Upvotes: 0
Reputation: 4536
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.
Upvotes: 2
Reputation: 779
I find the following steps in this order quite intuitive:
git branch -m old-name new-name
git push --set-upstream origin new-name
git push origin --delete old-name
If old-name is the default branch in the remote repository the deletion in step 3 will be rejected:
! [remote rejected] old-name (refusing to delete the current branch: refs/heads/old-name)
error: failed to push some refs to ...
You first have to change the default. On GitHub: Settings > Branches > Default branch
Upvotes: 7
Reputation: 2331
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 -u origin :old_name new_name
Upvotes: 217
Reputation: 141906
There are a few ways to accomplish that:
# Names of things - allows you to copy/paste commands
old_name=feature/old
new_name=feature/new
remote=origin
# Rename the local branch to the new name
git branch -m $old_name $new_name
# Delete the old branch on remote
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
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
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.
Upvotes: 2960
Reputation: 1086
Rename Local Branch
git branch -m new-name
git branch -m old-name new-name
Update Remote Branch
git push -d origin old-name
Set New Upstream
git push origin -u new-name
Upvotes: 24
Reputation: 2160
The easiest way to do this is just create a new branch off of your current branch and then push to remote. Then delete your previous branch (both local and remote).
Upvotes: 0
Reputation: 3863
git branch -m <old_name> <new_name>
Rename your local branch from
master
tolegacy
git branch -m master legacy
- 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 namedregacy
.git push origin legacy
pushes the local branch named legacy to the remote repository and creates a new remote branch namedlegacy
.
Upvotes: 45
Reputation: 2217
Renaming Git Branch Locally and Remotely:
Start by switching to the local branch which you want to rename:
git checkout <old_name>
Rename the local branch by typing:
git branch -m <new_name>
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>
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.
Upvotes: 37
Reputation: 21
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.
Upvotes: 2
Reputation: 4641
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.
Visit "Branches" in your repository settings: https://github.com/<name>/<repository-name>/settings/branches
Everyone using this repository has to do locally:
$ git fetch $ git checkout <new_name>
Upvotes: 6
Reputation: 1158
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>'
.
default
before deleting the branch (Github example)default
branch as shown in the picture below:$ git push origin :master
Upvotes: 2
Reputation: 860
Another workaround is the following:
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>
Upvotes: 6
Reputation: 4162
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
Stay on the target branch and:
git push origin :old-name new-name
Switch to the target branch and then:
git push origin -u new-name
Upvotes: 123
Reputation: 3767
If you have already pushed the wrong name to remote, do the following:
Switch to the local branch you want to rename
git checkout <old_name>
Rename the local branch
git branch -m <new_name>
Push the <new_name>
local branch and reset the upstream branch
git push origin -u <new_name>
Delete the <old_name>
remote branch
git push origin --delete <old_name>
This was based on this article.
Upvotes: 27
Reputation: 14467
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.
Upvotes: 26
Reputation: 7165
This can be done even without renaming the local branch in three simple steps:
Upvotes: 2
Reputation: 1219
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
Upvotes: 48
Reputation: 20137
There is no direct method,
Rename Local Branch,
My current branch is master
git branch -m master_renamed
#master_renamed is new name of master
Delete remote branch,
git push origin --delete master
#origin is remote_name
Push renamed branch into remote,
git push origin master_renamed
That's it...
Upvotes: 6
Reputation: 15577
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>
See the original answer for more detail.
Upvotes: 58
Reputation: 7945
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):
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
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 and then:
git push origin -u new-name
Upvotes: 784