in Git by
How do I rename a local Git branch?

I don't want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.

How can I rename a local branch which hasn't been pushed to a remote branch?

1 Answer

0 votes
If you want to rename a branch while pointed to any branch, do:

git branch -m <oldname> <newname>

If you want to rename the current branch, you can do:

git branch -m <newname>

A way to remember this is -m is for "move" (or mv), which is how you rename files. Adding an alias could also help. To do so, run the following:

git config --global alias.rename 'branch -m'

If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M, otherwise, git will throw branch already exists error:

git branch -M <newname>

Related questions

0 votes
asked Feb 13, 2022 in Git by rajeshsharma
0 votes
asked Oct 16, 2019 in Git by rajeshsharma
+1 vote
asked Jul 25, 2019 in Git by SakshiSharma
0 votes
asked May 21, 2020 in Git by SakshiSharma