Christian Giacomi

Git rename branch

Posted — Dec 20, 2020

This will be a very short post, and like the other git posts is meant primarily for me to remember a git command which I seldom use, but when I do, I never remember the exact syntax.

As they say:

If you want to remember somethinng, write it down

Rename a local branch

To do this you will need to use the branch command with the -m option and will have to specify the new name.

Renaming the current branch:

$ git branch -m <new-name>

Renaming a different branch:

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

If <new-name> already exists you will need to use the -M option instead to force the rename.

Rename a remote branch

If you have not previously pushed the old branch to your remote git repo then you can simply push like you would normally git push -u origin <new-name>

If instead you have previously pushed the old branch, then the simplest solution is to first delete the old branch and then push the new one.

$ git push origin --delete <old-name>

Or you can use the short form.

$ git push origin :<old-name>

And then push the new branch and add upstream tracking reference.

$ git push -u origin <new-name>
If this post was helpful tweet it or share it.

See Also