Rename a git branch

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:

  $ 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:

$ 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:

  $ 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

$ 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

$ git branch
* feature/adding-login
  master

$ git branch -m i-like-turtles

$ git branch
* i-like-turtles
  master
Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Leave a comment?

Leave a Reply