How do I rename a local Git branch?
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 which hasn't been pushed to a remote branch?
If you want to rename a branch while pointed to any branch, do:
If you want to rename the current branch, you can do:
A way to remember this, is -m
is for "move" (or mv
), which is how you rename files.
The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any.
If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):
git push origin new_branch_name:master
(now changes will go to master branch but your local branch name is new_branch_name)
For more details, see "How to rename your local branch name in Git."
To rename your current branch:
Here are the steps to rename the branch:
EDIT(12/01/2017) : Make sure you run command git status
and check that newly created branch is pointing to its own ref and not older one. If you find the reference to older branch, you need to unset the upstream using:
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:
If you are on a different branch:
The answers so far have been correct but here is some additional info:
One can rename a branch with '-m' (move), but one has to be careful, because '-M' 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.
If it is your current branch, just do
If it is another branch you want to rename
- If your branch was pushed, then after renaming you need to delete it from the remote Git repository and ask your new local to track a new remote branch:
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.
To rename a branch locally:
Now you'll have to propagate these changes on your remote server as well.
To push changes of the deleted old branch:
To push changes of creation of new branch:
Rename the branch using this command:
-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:
For more information about help, use this command in the terminal:
or
Advanced Git users can rename manually:
Probably as mentioned by others, this will be a case mismatch in branch naming.
If you have such a situation, I can guess that you're on Windows which will also lead you to:
Then you have to do an intermediate step:
Nothing more.
Here are three steps: A command that you can call inside your terminal and change branch name.
If you need more: step-by-step, How To Change Git Branch Name is a good article about that.
Trying to answer specifically to the question (at least the title).
You can also rename local branch, but keeps tracking the old name on the remote.
Now, when you run git push
, the remote old_branch
ref is updated with your local new_branch
.
You have to know and remember this configuration. But it can be useful if you don't have the choice for the remote branch name, but you don't like it (oh, I mean, you've got a very good reason not to like it !) and prefer a clearer name for your local branch.
Playing with the fetch configuration, you can even rename the local remote-reference. i.e, having a refs/remote/origin/new_branch
ref pointer to the branch, that is in fact the old_branch
on origin
. However, I highly discourage this, for the safety of your mind.
To rename current branch (except for detached HEAD state) you can also use this alias:
Another option is not to use the command line at all. Git GUI clients such as SourceTree take away much of the syntactical learning curve / pain that causes questions such as this one to be amongst the most viewed on Stack Overflow.
In SourceTree, right click on any local branch in the "Branches" pane on the left and select "Rename ...".
If you are willing to use to use SourceTree (which I strongly recommend) you can right click your branch and chose 'Rename'.
Since you do not want to push the branch to a remote server, this example will be useful:
Let's say you have an existing branch called "my-hot-feature," and you want to rename it to "feature-15."
First, you want to change your local branch. This couldn't be easier:
For more information, you can visit Locally and Remotely Renaming a Branch in Git.
Changing the branch locally is quite easy...
If you are on the branch you want to change the name, simply do this:
Otherwise, if you are on master
or any other branch other than the one you'd like to change the name, simply do:
Also, I create the image below to show this in action in a command line, in this case, you are on master
branch for example:
git version 2.9.2
If you want to change the name of the local branch you are on:
If you want to change the name of a different branch:
If you want to change the name of a different branch to a 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.
If you are on the branch you want to rename:
If you are on a different branch:
git push origin :old-name new-name
git push origin -u new-name
Or you as a fast way to do that, you can use these 3 steps: command in your terminal
# Rename branch locally
# Delete the old branch
# Push the new branch, set local branch to track the new remote
Referance: https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html
If you want to change the name of the current branch, run:
If you want to delete the old remote branch, run:
If you want to delete the old remote branch and create a new remote branch, run:
Simple way to do it :
For more, see this.
All of the above are talking about git branch -m
.Of course, it's easy to operate, but for me, it may be a little hard to remember another git command. So I tried to get the work down by the command I was familiar with.Yeah, you may guessed it.
I use git branch -b <new_branch_name>
.And if you don't want to save the old branch now you can execute git branch -D <old_branch_name>
to remove it.
I know it may be a little tedious but it's easier to understood and remember.Hope it‘s helpful for you.
git branch rename can be done by using
git branch -m oldBranch newBranch
git branch -M oldBranch ExistingBranch
Difference between -m and -M ,
-m: if your trying to rename your branch with exising branch name using -m
it will raise an error says, branch already exist. you need to give unique name.
but,
-M: this will help you to force rename with given name even it is exist. so existing branch will overwrite entirely with it...
Here is git terminal example,
Rename branch:
git branch -m old_branchname new_branchname
here The long name of the -m option is --move. So we can also use
git branch --move old_branchname new_branchname
If you wan to rename the current branch thrn use this.
or
If you want to move this changes to remote Then use the following.
This will delete the old_branchname remote branch and push the new_branchname local branch.
This will reset the upstream branch for the new_branchname local branch.
PHPStorm:
VCS->Git->Branches...->Local Branches->_your_branch_->Rename
If you want to:
git branch -m old_branch_name new_branch_name
OR
git branch --move old_branch_name new_branch_name
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?