git branch -m <oldname> <newname> # Any Branch
git branch -m <newname> # Current Branch
# For windows if you get "Branch already exists" error
git branch -M <newname>
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>
1. 'Start by switching to the local branch which you want to rename:'
git checkout <old_name>
2. 'Rename the local branch by typing:'
git branch -m <new_name>
# Rename the local branch to the new name
git branch -m <old_name> <new_name>
# Delete the old branch on remote - where <remote> is, for example, origin
git push <remote> --delete <old_name>
# Or shorter way to delete remote branch [:]
git push <remote> :<old_name>
# Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of <new_name>.
git branch --unset-upstream <new_name>
# Push the new branch to remote
git push <remote> <new_name>
# Reset the upstream branch for the new_name local branch
git push <remote> -u <new_name>
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>
// Make sure you are in the project folder
// 1. go to your branch that you want to rename
$ git branch -m <new_branch_name>
// 2. if you are in the main branch
$ git branch <old_branch_name> <new_branch_name>
// ### you should check by git command : $ git branch : to check new branch name