Is there a way to rename a Git branch locally and push it to the remote branch, even if there are already many commits pushed to the remote branch?
Or, is it necessary to create a new local branch, delete the old local branch, and then repeat the operation on the remote repository?
move exists to rename the branch locally
git branch --move
but to push it, you must delete the old and push the new
git push origin [--set-upstream]
git push origin --delete
--set-upstreamis optional, it configure the new local branch to track the pushed one
You can use the following shorthands:
- move locally (–move) :
Default12git branch -m
- push new branch (–set-upstream, optional) :
Default12git push origin [-u]
- delete (–delete) :
Default12git push origin -d
Thanks to torek’s comment:
Worth mentioning, by the way, is that you should
- notify other users who share the upstream that you will be doing this, and
- do this in the order shown (set new name, then delete old).
The reason for #1 is that those users will need to adjust.
The reason for #2 is mainly just efficiency: it avoids having to re-copy objects to an upstream repo that drops commits on branch deletion (most bare repositories do that, and most repositories that accept pushes are bare)