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 wh
Rename the branch using this command:
git branch -m [old_branch_name] [new_branch_name]
-m
: It renames/moves the branch. If there is already a branch, you will get an error.
If there is already a branch and you want to rename with that branch, use:
git rename -M [old_branch_name] [new_branch_name]
For more information about help, use this command in the terminal:
git branch --help
or
man git branch
Actually you have three steps because the local branch has a duplicate on the server so we have one step for local on two steps on the server:
git branch -m <old-branch-name> <new-branch-name>
git push <remote-name[origin by default]> :<old-branch-name>
git push -u <new-branch-name>
Git branch rename can be done by using:
git branch -m oldBranch newBranch
git branch -M oldBranch ExistingBranch
The difference between -m and -M:
-m: if you're trying to rename your branch with an existing branch name using -m. It will raise an error saying that the branch already exists. You need to give unique name.
But,
-M: this will help you to force rename with a given name, even it is exists. So an existing branch will overwrite entirely with it...
Here is a Git terminal example,
mohideen@dev:~/project/myapp/sunithamakeup$ git branch
master
master0
new_master
test
* test1
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -m test1 test
fatal: A branch named 'test' already exists.
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -M test1 test
mohideen@dev:~/project/myapp/sunithamakeup$ git branch
master
master0
new_master
* test
mohideen@dev:~/project/myapp/sunithamakeup$
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
git push origin :old-name new-name
git push origin -u new-name
Or for a fast way to do that, you can use these 3 steps:
# Rename branch locally
git branch -m old_branch new_branch
# Delete the old remote branch
git push origin :old_branch
# Push the new branch, set local branch to track the new remote
git push --set-upstream origin new_branch
Referance: https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html
I foolishly named a branch starting with a hyphen, and then checked out master. I didn't want to delete my branch, I had work in it.
Neither of these worked:
git checkout -dumb-name
git checkout -- -dumb-name
"
s, '
s and \
s didn't help either. git branch -m
doesn't work.
Here's how I finally fixed it. Go into your working copy's .git/refs/heads, find the filename "-dumb-name", get the hash of the branch. Then this will check it out, make a new branch with a sane name, and delete the old one.
git checkout {hash}
git checkout -b brilliant-name
git branch -d -- -dumb-name
Advanced Git users can rename manually using:
Rename the old branch under .git/refs/heads to the new name
Rename the old branch under .git/logs/refs/heads to the new name
Update the .git/HEAD to point to yout new branch name