How can I rename a local branch which has not yet been pushed to a remote repository?
Related:
How can I rename a local branch which has not yet been pushed to a remote repository?
Related:
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> git push -f --mirror, then it will rename the branch on the remote, but you should only use this method if the remote is simply to be a copy of your current repository. See also this question: stackoverflow.com/questions/1526794/git-rename-remote-branchpush.default is configured. By default (matching) it will push to a remote whose name matches. You would have to do git push origin <newname>:<oldname> or you will create a new remote branch. However, if push.default is set to upstream, then you can push origin head and things will go to the oldname on the remote.-m option is --move, e.g., git branch --move master renames the current branch to be called "master".git push origin :<old_name> <new_name> to update your remote branch names.git push -f --mirror! ONLY do this if the remote is a copy of your current repository. If it is a shared team repository... our team just learned that all (several hundred) open PRs were closed by this script, and then all (several hundred) open branches were deleted!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.
To rename your current branch:
git branch -m <newname> git push origin HEAD:<oldname>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 -m), 3 = push 'nothing' to the old branch destination on the remote (i.e. delete the reference to the branch on the remote) - left side of a colon is 'source', right side is 'destination', 4 = push a reference (pointer) to the new branch, to the remotegit branch --unset-upstream resolves the unsynchronised condition(s) to which you're referring?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 git push origin :old-name new-name right?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.
-M flag is also useful to force a rename if you are just correcting the case of the branch name, e.g. changing myBranch to MyBranch. (With -m, git returns fatal: A branch named 'MyBranch' already exists.)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.
Update 2025
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 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 In the end, as Nicolas Castro explained in the comments, to reset the upstream, run two commands respectively.
git branch --unset-upstream git push --set-upstream origin new-name git branch --unset-upstream and later a git push --set-upstream origin branch_name to get everything clean.If it is your current branch, just do
git branch -m new_name If it is another branch you want to rename
git branch -m old_name new_name - 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:
git push origin :old_name git push --set-upstream origin new_name old_name branch is the default branch, you may get an error message like "The default branch of a project cannot be deleted". In that case, you need to git push the new_name branch creation first, change the default branch to it, and then git push the old_name branch removal.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 checkout that name, but can delete it.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
git push --set-upstream origin new_branchname which is mentioned in @Nomade answerTrying to answer specifically the question (at least the title).
You can also rename the local branch, but keep tracking the old name on the remote.
git branch -m old_branch new_branch git push --set-upstream origin new_branch:old_branch 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.
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 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 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
Here are three steps: A command that you can call inside your terminal and change branch name.
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote If you need more: step-by-step, How To Change Git Branch Name is a good article about that.
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:
$ git branch -m CaseSensitive casesensitive fatal: A branch named 'casesensitive' already exists. Then you have to do an intermediate step:
$ git branch -m temporary $ git branch -m casesensitive Nothing more.
-M instead of -m to do this kind of "casing fix" rename in a single step.For more details on this procedure.
To rename the current branch, make sure you’ve checked out and are using the branch you want to rename.
git checkout oldbranch
And then
git branch -m newbranch
If you want to, you can rename a branch when you’re working in another branch.
git branch -m oldbranch newbranch
If others use this branch and commit to it, you should pull it before renaming it locally. This ensures that your local repository is updated and that changes made by other users will not be lost.
First, we need to delete oldbranch from the remote repository, and push newbranch to the remote.
git push origin --delete oldbranch
Now we’ll push the new one to the remote, by using -u (set upstream) option.
git push origin -u newbranch
Changing the branch locally is quite easy...
If you are on the branch you want to change the name for, simply do this:
git branch -m my_new_branch Otherwise, if you are on master or any other branch other than the one you'd like to change the name, simply do:
git branch -m my_old_branch my_new_branch Also, I create the image below to show this in action on a command line. In this case, you are on master branch, for example:
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:
git branch -m my-hot-feature feature-15 For more information, you can visit Locally and Remotely Renaming a Branch in Git.
If you are willing to use SourceTree (which I strongly recommend), you can right click your branch and chose 'Rename'.
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 ...".
A simple way to do it:
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote For more, see this.
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.
If you want to change the name of the current branch, run:
git branch -m [old_branch] [new_branch] If you want to delete the old remote branch, run:
git push origin :[old_branch] If you want to delete the old remote branch and create a new remote branch, run:
git push origin :old_branch new_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$ All of the previous answers 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 done 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 understand and remember. I hope it‘s helpful for you.