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!
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.
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