How do I rename a local Git branch?

Explanation

So if you want to rename a branch but by pointing to any other branch. At that time we will use 

git branch -m <oldname> <newname>

 

And if you want a rename the current branch. At that time we will use a method 

git branch -m <newname>

 

So in case you want to push the local branch. And reset the upstream branch. So we will use

git push origin -u <newname>

 

And so in case you want to Delete the remote branch: At that time use

git push origin --delete <oldname>

 

So it is hard to remember. Use the methods that is  -m is for “move” (or mv). So this is how you rename files.  To do so, use the below code:

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

 

If you are on Windows or another case-insensitive file system, and there are only capitalization changes in the name, you need to use -M otherwise, git will show an error. That is branch already exists error:

git branch -M <newname>

 

Also read,  Entity relationship diagram with only crow’s foot notation

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *