If there is a repository that I only have git:// access to (and would usually just push+pull), is there a way to rename branches in that repository in the same way that I would do locally with git branch -m?
-
I tried all the below methods. Essentially, you cannot rename the remote branch. All below methods are deleting old remote branch and create a new remote one. If you do so in gitlab, the old branch/MR will be closed and you have to create a new MR.new2cpp– new2cpp2018-02-23 07:09:46 +00:00Commented Feb 23, 2018 at 7:09
-
Related: Rename master branch for both local and remote Git repositoriesVadzim– Vadzim2019-10-01 10:23:32 +00:00Commented Oct 1, 2019 at 10:23
-
Also related: How do I rename both a Git local and remote branch namejoanis– joanis2021-11-17 13:30:20 +00:00Commented Nov 17, 2021 at 13:30
10 Answers
You just have to create a new local branch with the desired name, push it to your remote, and then delete the old remote branch:
$ git branch new-branch-name origin/old-branch-name
$ git push origin --set-upstream new-branch-name
$ git push origin :old-branch-name
Then, to see the old branch name, each client of the repository would have to do:
$ git fetch origin
$ git remote prune origin
NOTE: If your old branch is your main branch, you should change your main branch settings. Otherwise, when you run $ git push origin :old-branch-name, you'll get the error "deletion of the current branch prohibited".
11 Comments
git fetch origin --prune (to effectively fetch the new branches and also get rid of the references no longer on the remote).-d or --delete instead of : in newer versions of git.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:
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
I wrote this script (git-rename-remote-branch) which provides a handy shortcut to do the above easily.
As a bash function:
git-rename-remote-branch() {
if [ $# -ne 3 ]; then
echo "Rationale : Rename a branch on the server without checking it out."
echo "Usage : ${FUNCNAME[0]} <remote> <old name> <new name>"
echo "Example : ${FUNCNAME[0]} origin master release"
return 1
fi
git push $1 $1/$2\:refs/heads/$3 :$2
}
To integrate @ksrb's comment: What this basically does is two pushes in a single command, first git push <remote> <remote>/<old_name>:refs/heads/<new_name> to push a new remote branch based on the old remote tracking branch and then git push <remote> :<old_name> to delete the old remote branch.
16 Comments
git push <remote>/<old_name>:refs/heads/<new_name> means push a new remote that uses the old remote as a src then git push [space]:<old_name> means delete the old remoterefs/heads/name? Can't you just use name directly, making the first command git push <remote> <remote>/<old_name>:<new_name>?<new_name> does not exist yet. If the branch does not exist, Git requires you to use the full name as otherwise <new_name> could also refer to a tag name.refs/heads/<new_name> already exists. The delete still succeeds, resulting in <remote>/<old_name> only being deleted. Some checking before hand can easily avoid this.First checkout to the branch which you want to rename:
git branch -m old_branch new_branch
git push -u origin new_branch
To remove an old branch from remote:
git push origin :old_branch
2 Comments
git push -u origin new_branch) otherwise the renamed branch (new_branch) will continue to track the origin/old_branch. And once you delete the remote old_branch, the new_branch will still track the origin/old_branch, albeit now that branch is gone.Sure. Just rename the branch locally, push the new branch, and push a deletion of the old.
The only real issue is that other users of the repository won't have local tracking branches renamed.
1 Comment
I don't know if this is right or wrong, but I pushed the "old name" of the branch to the "new name" of the branch, then deleted the old branch entirely with the following two lines:
git push origin old_branch:new_branch
git push origin :old_branch
1 Comment
TL;DR
"Renaming" a remote branch is actually a 2 step process (not necessarily ordered):
- deletion of the old remote branch (
git push [space]:<old_name>as ksrb explained); - push into a new remote branch (difference between a couple of answers commands below).
Deleting
I use TortoiseGit and when I first tried to delete the branch through the command line, I got this:
$ git push origin :in
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
This was likely due to pageant not having the private key loaded (which TortoiseGit loads automatically into pageant). Moreover, I noticed that TortoiseGit commands do not have the origin ref in them (e.g. git.exe push --progress "my_project" interesting_local:interesting).
I am also using Bitbucket and, as others web-based online git managers of the sort (GitHub, GitLab), I was able to delete the remote branch directly through their interface (branches page):
However, in TortoiseGit you may also delete remote branches through Browse References:
By right-clicking on a remote branch (remotes list) the Delete remote branch option shows up:
Pushing
After deleting the old remote branch I pushed directly into a new remote branch through TortoiseGit just by typing the new name in the Remote: field of the Push window and this branch was automatically created and visible in Bitbucket.
However, if you still prefer to do it manually, a point that has not been mentioned yet in this thread is that -u = --set-upstream.
From git push docs, -u is just an alias of --set-upstream, so the commands in the answers of Sylvain (-set-upstream new-branch) and Shashank (-u origin new_branch) are equivalent, since the remote ref defaults to origin if no other ref was previously defined:
git push origin -u new_branch=git push -u new_branchfrom the docs description:If the configuration is missing, it defaults to
origin.
In the end, I did not manually type in or used any of the commands suggested by the other answers in here, so perhaps this might be useful to others in a similar situation.
5 Comments
origin. You have to name your remote as you get it from running the command git remote. Git works with ssh what implies that you're using public+private keys. I assume that the Autoload Putty keys of TortoiseGit is just autoloading the needed keys for you to do anything at all with your remote reference. Last thing is that git push -u is not an alias for pushing into a remote branch, it is an alias for pushing into a remote branch that was created locally and its remote reference has not yet this branch.-u is an alias of --set-upstream and "if the configuration is missing, it defaults to origin". Sylvain and Shashank use this for pushing into a newly created remote branch. The key issue may have been due to pageant not having it loaded when I tried git push origin :in on the shell. So I don't understand your downvote, I just pointed out mine and non-addressed details in other answers, explained them and solved them.-u is an alias for --set-upstream but that is not an alias for pushing into a remote branch as you said. To push into a remote branch you uniquely need git push <remote>, and if it's not in the remote yet, you add git push -u <remote>. Therefore, -u is used to create a reference of the branch in the remote.I don't know why but @Sylvain Defresne's answer does not work for me.
git branch new-branch-name origin/old-branch-name
git push origin --set-upstream new-branch-name
git push origin :old-branch-name
I have to unset the upstream and then I can set the stream again. The following is how I did it.
git checkout -b new-branch-name
git branch --unset-upstream
git push origin new-branch-name -u
git branch origin :old-branch-name
Comments
If you're using Github...
You can create a new branch based on old-name branch using the UI on github.com:
1 Comment
Adding to the answers already given, here is a version that first checks whether the new branch already exists (so you can safely use it in a script)
if git ls-remote --heads "$remote" \
| cut -f2 \
| sed 's:refs/heads/::' \
| grep -q ^"$newname"$; then
echo "Error: $newname already exists"
exit 1
fi
git push "$oldname" "$remote/$oldname:refs/heads/$newname" ":$oldname"
(the check is from this answer)
1 Comment
git show-ref --quiet --verify -- refs/heads/$new_name instead of ls-remote | cut | sed | grep.


