How can I rename a local Git branch?
Solution 1
To rename the current branch:
git branch -m <newname>
To rename a branch while pointed to any branch:
git branch -m <oldname> <newname>
-m
is short for --move
.
To push the local branch and reset the upstream branch:
git push origin -u <newname>
To delete the remote branch:
git push origin --delete <oldname>
To create a git rename
alias:
git config --global alias.rename 'branch -m'
On Windows or another case-insensitive filesystem, use -M
if there are only capitalization changes in the name. Otherwise, Git will throw a "branch already exists" error.
git branch -M <newname>
Solution 2
You can rename a local Git branch using the following command:
git branch -m old_branch_name new_branch_name
Keep in mind that when you rename a branch, it still maintains its association with the old upstream branch if there was one.
To push changes to the master
branch after renaming your local branch to new_branch_name
, use the following command:
git push origin new_branch_name:master
With this command, your changes will be pushed to the master
branch on the remote repository. However, your local branch will still be named new_branch_name
.
For more details, see: How to rename your local branch name in Git.
Solution 3
To rename your current branch:
git branch -m <newname>
Solution 4
Here are the steps to rename the branch:
- Switch to the branch which needs to be renamed
git branch -m <new_name>
git push origin :<old_name>
git push origin <new_name>:refs/heads/<new_name>
EDIT (12/01/2017): Make sure you run command git status
and check that the newly created branch is pointing to its own ref and not the older one. If you find the reference to the older branch, you need to unset the upstream using:
git branch --unset-upstream
Solution 5
Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one.
From my experience, to rename a local and remote branch in Git you should do the following steps.
Quoting from Multiple States - Rename a local and remote branch in git
1. Rename your local branch
If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
2. Delete the old-name remote branch and push the new-name local branch
git push origin :old-name new-name
3. Reset the upstream branch for the new-name local branch
git push origin -u new-name