
How do I rename a local Git branch?
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>
If you want to push the local branch and reset the upstream branch:
git push origin -u <newname>
And finally if you want to Delete the remote branch:
git push origin --delete <oldname>
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>
For more info: https://git-scm.com/docs/git-branch/2.28.0
Related articles
Mastering Git Bisect: A Developer's Guide to Efficient Debugging
Dive deep into the world of Git Bisect to uncover how this powerful tool can revolutionize your debugging process, making it more efficient and less time-consuming.
Git Stash
Git stash is a built-in command with the distributed Version control tool in Git that locally stores all the most recent changes in a workspace and resets the state of the workspace to the prior commit state.
Git Branching
Branching is a feature available in most modern version control systems.