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
The answers so far have been correct, but here is some additional information:
One can safely rename a branch with '-m' (move), but one has to be careful with '-M', because it forces the rename, even if there is an existing branch with the same name already. Here is the excerpt from the 'git-branch' man page:
With a -m or -M option,
<oldbranch>
will be renamed to<newbranch>
. If<oldbranch>
had a corresponding reflog, it is renamed to match<newbranch>
, and a reflog entry is created to remember the branch renaming. If<newbranch>
exists, -M must be used to force the rename to happen.
Here are the steps to rename the branch:
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
To rename your current branch:
git branch -m <newname>
To rename the current branch (except for detached HEAD state) you can also use this alias:
[alias]
mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'
Git version 2.9.2
If you want to change the name of the local branch you are on:
git branch -m new_name
If you want to change the name of a different branch:
git branch -m old_name new_name
If you want to change the name of a different branch to a name that already exists:
git branch -M old_name new_name_that_already_exists
Note: The last command is destructive and will rename your branch, but you will lose the old branch with that name and those commits because branch names must be unique.
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 three 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 commands:
git push origin --delete old-name
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