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
"Renaming" a remote branch is actually a 2 step process (not necessarily ordered):
git push [space]:<old_name>
as ksrb explained);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:
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_branch
from 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.
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
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.
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
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".
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)