Question

How to rename a local Git branch via the command line?

More often than not, I would create a new branch with git checkout -b branch_name and I would realize that I’ve made a typo or I would come up with a better name for the branch later on.

If I have just created the branch it is ok as I can create a new one, but sometimes I would notice this after a couple of commits.

So here’s how you could rename a local Git branch via your command line!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 6, 2020
Accepted Answer

If you want to test this on a fresh new repository, then you can do the following prep work:

  • Quickly create a new folder and cd into it:
  1. mkdir my-repo ; cd my-repo
  • Then initialize a new git repository:
  1. git init .
  • After that create a new branch:
  1. git checkout -b wrong-branch-name
  • Then check your the available branches with the following command:
  1. git branch

Output:

  main
* wrong-branch-name
  • Then similar in order to rename the branch you could just run the following command:
  1. git branch -m wrong-branch-name correct-branch-name
  • If you want to rename your current branch, you could just run the following:
  1. git branch -m my-branch-name

After that, if you run git branch again you will be able to see the correct branch name!

An easy way to remember the -m flag is from the mv command in Linux, the mv command is used to move files but also rename them, just like the -m flag for Git!

This is pretty much it! Hope that this helps. Regards, Bobby

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.