By Bobby Iliev
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!
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!
Accepted Answer
If you want to test this on a fresh new repository, then you can do the following prep work:
cd
into it:- mkdir my-repo ; cd my-repo
- git init .
- git checkout -b wrong-branch-name
- git branch
Output:
main
* wrong-branch-name
- git branch -m wrong-branch-name correct-branch-name
- 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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.