How do I rename a local branch which has not yet been pushed to a remote repository?
Related:
How do I rename a local branch which has not yet been pushed to a remote repository?
Related:
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
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
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
Just three steps to replicate change in name on remote as well as on GitHub:
Step 1 git branch -m old_branchname new_branchname
Step 2 git push origin :old_branchname new_branchname
Step 3 git push --set-upstream origin new_branchname
Update 2022
Before we begin, make sure you’ve selected the branch you want to rename:
git checkout old-name
If you want to see all of your local branches, use the following command:
git branch --list
When you’re all clear, follow these steps:
Using the Git rename branch command will require you to add an -m option to your command:
git branch -m new-name
You can also rename a local branch from another branch by using the following two commands:
git checkout master
git branch -m old-name new-name
Lastly, this command will list all — both local and remote — branches to verify that it has been renamed:
git branch -a
Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these two easy steps:
To start, you will need to rename a local branch by following the previous steps. 2.Then delete the old branch and push the new one. You can do this easily with the following command:
git push origin :old-name new-name
Reset the upstream branch for your new local branch, and you will be all set:
git push origin -u new-name
git branch -m <new_name>
This will set the new name for the current branch you are working with.
git branch -m <old_name> <new_name>
Here you have to provide the old branch name and the new branch name.
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>
For Git GUI users it couldn't be much simpler. In Git GUI, choose the branch name from the drop down list in the "Rename Branch" dialog box created from the menu item Branch:Rename, type a New Name, and click "Rename". I have highlighted where to find the drop down list.
To rename a local branch on GitHub Desktop, click on the Current Branches tab (to the right of current repository), right click on the branch you want to rename and click on rename. You will then be prompted with a popup where you can rename.
Offical Command from Git Itself. Try this one. It works for me.
The default branch has been renamed! {oldBranchName} is now named {NewBranchName} If you have a local clone, you can update it by running the following commands.
git branch -m {oldBranchName} {NewBranchName}
git fetch origin
git branch -u origin/{NewBranchName} {NewBranchName}
git remote set-head origin -a
All you have to do are the three steps:
.git/refs/heads the new name.git/logs/refs/heads the new name.git/HEAD to lead to your new branch nameRename a local branch:
git branch -m <old_branch_name> <new_branch_name>
Push the new branch:
git push --set-upstream origin <new_branch_name>
git branch -m [old-branch] [new-branch]
-m means move all from [old-branch] to [new-branch] and remember you can use -M for other file systems.
In Visual Studio:
Git → Manage Branches → Branches → your_repository → your_branch → Rename