Rename a git branch

02 Oct 2015 in Unix Toolbox

I can't count the number of times that I've created a new feature branch only to notice that I've managed to add a typo to the branch name. Usually, it looks something like this:

bash
$ git checkout -b feature/addign-login

Usually in this situation I'd create a new branch with the correct name from my current (incorrectly named) branch and delete the old one:

bash
$ git checkout -b feature/adding-login
$ git branch -D feature/addign-login

It turns out that this happens enough for git to have an alias for it - git branch -m.

Instead of creating a new branch and deleting the old one, you can do the following:

bash
$ git branch -m feature/adding-login

This will rename the branch that you're on in place.

If you want to see a more complete example, here I fix the typo running git branch before and after to show you the state of branches before and after

bash
$ git branch
* feature/addign-login
master
$ git branch -m feature/adding-login
$ git branch
* feature/adding-login
master

You can of course use any name that you like

bash
$ git branch
* feature/adding-login
master
$ git branch -m i-like-turtles
$ git branch
* i-like-turtles
master