Orion42 Blog

~IT made easy~

Linux, Bash and Git

[Git] Rename a local and remote branch

If you want to rename a git branch on both your local and remote repository, you can change its name using the -m parameter of the branch command.

Rename your local branch.
You can do it both from inside the <old-name> branch

git checkout <old-name>
git branch -m <new-name>

or from the outside, specifying both the <old-name> and the <new-name>

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>

This expression is the merge of two different instruction: the addition of a new remote <new-name> branch and the deletion of the remote <old-name> branch, which can be accomplished in different ways depending on your git version.

Before Git 2.7, a column was used:

git push <remote_name> :<branch_name>

From Git 2.7, it can be also written with the --delete parameter

git push <remote_name> --delete <branch_name>

With Git 2.8, the alias -d was introduced for --delete

git push <remote_name> --d <branch_name>

3. Reset the upstream branch for the <new-name> local branch.
Switch to the branch and then:

git push origin -u <new-name>

From git help push:

-u, --set-upstream.  For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).”


Leave a Reply