Renaming A Git Branch

Git

24/04/2021


You can rename a branch with

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

If you are already on the branch you wish to rename, you can skip the first argument.

BASH
git branch -m <new branch name>

In both cases, -m is shorthand for --move.

Renaming a remote branch

Unfortunately, 🙁 there is no direct way to rename a remote branch. As a work around, you can delete the remote branch and then push the new renamed local branch to your repository.

Assuming you've already renamed your local branch, you can do so in one fell swoop.

BASH
git push origin :<old branch name> <new branch name>

After that, you will need to reset the upstream branch,

BASH
git push origin –u <new branch name>

where -u is shorthand for --set-upstream.


WRITTEN BY

Code and stuff