Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share

19 Answers 19

up vote 7658 down vote accepted

If you want to rename a branch while pointed to any branch, do:

git branch -m <oldname> <newname>

If you want to rename the current branch, you can do:

git branch -m <newname>

A way to remember this, is -m is for "move" (or mv), which is how you rename files.

share
20  
What I really wanted to know was whether this will necessarily effect the remote branch when/if you push – PandaWood Jan 23 '12 at 0:15
65  
@PandaWood: it will add the new branch when you push, but won't delete the old branch. If you use 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-branch – siride Jan 23 '12 at 6:02
10  
@PandaWood, it depends on how push.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. – Erin Stanfill Oct 31 '13 at 23:46
7  
@NightOwl888: the -m probably is short for "move", following the Unix convention of using the mv to rename files. The reason for this is that moving and renaming, in a directory-based inode file system, are entirely equivalent. – siride Sep 3 '14 at 2:27
16  
The long name of the -m option is --move, e.g., git branch --move master renames the current branch to be called "master". – robenk Sep 22 '15 at 17:56
git branch -m old_branch_name new_branch_name

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."

share

To rename your current branch:

git branch -m <newname>
share
64  
You will need to use -M to rename if you are only changing capitalization, as git will tell you that branch already exists. – cjspurgeon May 8 '15 at 21:04

Here are the steps to rename the branch:

1. switch to branch which needs to be renamed
2. git branch -m <new_name>
3. git push origin :<old_name>
4. git push origin <new_name>:refs/heads/<new_name>

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:

git branch --unset-upstream
share

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.

share
2  
What happens to the overwritten branch? – Kevin Dice Jun 22 '15 at 20:06
    
It is overwritten by the new name/branch. For example if you have the following branches in git: master b1 <-- current branch b2 after you do 'git branch -M b2' you will only have: master b2 <-- current branch b1 will be gone and if you wish to recover it you should check it out by its hash. You can see it by typing 'git reflog'. Cheers. – Vanchev Jun 26 '15 at 16:48

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
share
5  
Couldn't you just have renamed the file in refs/heads? – android.weasel Nov 13 '13 at 18:07
    
Ditto. If you have to dig into the directory structure to do this magic, go all the way and do a 'mv -- -dumb-name brilliant-name' Do a 'git branch -av' and you'll see an directory structure of .git/refs. Or maybe 'grep -R ^ .git/refs' to see the hashes directly. – Dave X Dec 19 '13 at 17:15
2  
You could probably have used reflog – Code Whisperer Jan 14 '15 at 20:27
    
Honestly, if that's the route you wanted to take, I'd avoid the (IMO confusing and potentially dangerous if you don't know what you're doing) jaunt through .git directory in the first place, and just do it with some normal commands with some "git log" parsing (using appropriate flags to show branches, and to figure out which shasum you want to checkout a new branch from), and then do it. Then, remove the wonky-named branch. I despise that git insists that you need to understand all of its inner workings to do some things, but greatly appreciate that you can do those things. – Jon V Feb 27 '16 at 14:37
    
It's harder to create a branch with a bad name in 2.10.1+. If you do somehow do it, you can use git branch -v to get the short hash version of your branches(add -r for remote). You can then use git rev-parse <shorthash> to get the full hash if you need it. – House of Dexter Mar 6 at 17:19

To rename a branch locally:

git branch -m [old-branch] [new-branch]

Now you'll have to propagate these changes on your remote server as well.

To push changes of the deleted old branch:

git push origin :[old-branch]

To push changes of creation of new branch:

git push origin [new-branch]
share
  • 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
    
share

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

1. Rename your local branch

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

2. Delete the old-name remote branch and push the new-name local branch

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch

git push origin -u new-name
share
3  
This should be the accepted answer – Nam G VU Dec 8 '16 at 6:33

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
share

Advanced Git users can rename manually:

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
share

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.

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.

share

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 ...".

share
4  
I wouldn't call it pain. The git command is very easy to use, once you've seen this answer, you'll probably never come back again. The problem is more that, so it seems, the documentation of the git command-line isn't intuitive enough. – Nearoo Mar 8 '15 at 17:05
1  
True but with SourceTree I hardly ever need to worry about checking documentation. Everything is generally intuitive - just right click and see what the options are. (BTW I'm not affiliated with them in any way - just like the tool!) – Steve Chambers Mar 8 '15 at 17:17

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.

share
1  
Note that this situation might also arise on a Mac, which is also (exceptionally annoyingly) case insensitive in its file system. – Jon V Feb 27 '16 at 14:41

To rename 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'
share

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.

share

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.

share

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.

share

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
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.