9328

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?

In case you need to rename remote branch as well:
How do I rename both a Git local and remote branch name

Sazzad Hissain Khan
  • 29,428
  • 20
  • 134
  • 192
Forrest
  • 101,971
  • 19
  • 69
  • 107

34 Answers34

15119

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>

If you want to push the local branch and reset the upstream branch:

git push origin -u <newname>

And finally if you want to Delete the remote branch:

git push origin --delete <oldname>

A way to remember this is -m is for "move" (or mv), which is how you rename files. Adding an alias could also help. To do so, run the following:

git config --global alias.rename 'branch -m'

If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M, otherwise, git will throw branch already exists error:

git branch -M <newname>
Numan Gillani
  • 338
  • 3
  • 13
siride
  • 165,323
  • 4
  • 31
  • 54
  • 102
    What I really wanted to know was whether this will necessarily effect the remote branch when/if you push – PandaWood Jan 23 '12 at 00:15
  • 206
    @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: http://stackoverflow.com/questions/1526794/git-rename-remote-branch – siride Jan 23 '12 at 06:02
  • 24
    @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 :` 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
  • 15
    @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 03 '14 at 02:27
  • May not work on Windows. Git on Windows complains the branch already exists. – Sam Rueby Mar 30 '15 at 12:23
  • 2
    @Sam.Rueby: I am not able to reproduce that error. It works fine for me. I'm using msysgit (git version 1.9.5.msysgit.0) on Windows 7, 64-bit. – siride Mar 30 '15 at 14:12
  • 55
    The long name of the `-m` option is `--move`, e.g., `git branch --move master` renames the current branch to be called "master". – robenkleene Sep 22 '15 at 17:56
  • interesting, it seems that the rename is temporarily case insensitive, even on OSX. Starting from **FB12.show_hide_cols**, **-m fb12.show_hide_cols** got me an existing branch error. But **-m fb12.show_hide_col**, then **-m fb12.show_hide_cols** got me where I wanted. afterwards, **git branch** only shows the one renamed branch, just like I wanted. – JL Peyret Mar 22 '16 at 01:01
  • 3
    After doing this, you should also run `git push origin : ` to update your remote branch names. – David Meza Apr 01 '16 at 14:52
  • 4
    @Sam.Rueby You will need to use -M to rename if you are only changing capitalization, as git will tell you that branch already exists. – Chiel ten Brinke Jul 01 '16 at 10:09
  • 11
    for those who stumble upon this thread and comment, be wary when considering using `git push -f --mirror`. this will also push *all* local branches to remote as well, including any unpruned branched. i did this and pushed 30+ old branches to remote...whoops! – liltitus27 Dec 01 '16 at 17:00
  • 8
    Also be **very wary** of `git push -f --mirror` because it will quite literally "mirror" your local repo. This may be fine if you are working alone but if you are contributing to a code base with a team `git push -f --mirror` will delete any branches that aren't on your local. – Bueno Sep 05 '18 at 20:47
  • @HaFizUmer This will only rename your local branches. If you want to propagate those changes to another repository, such as a GitHub repository, you will need to run `git push origin :old_branchname new_branchname` (see Harry_pb's answer below). – siride Mar 26 '19 at 21:55
  • Nitpicking: Windows' file system ([NTFS](https://en.wikipedia.org/wiki/NTFS)) is in fact case-sensitive. It is provisioned for systems that choose to expose the file system in a case-insensitive fashion. Windows does that. – IInspectable Dec 16 '19 at 11:57
  • is it possible that the old branch is not deleted in windows if i use -m, and same branch having two names and if i commit in new branch it goes ahead from older one https://stackoverflow.com/questions/61367248/git-some-how-a-single-branch-having-two-names-created-how-do-i-make-it-single/61369981#61369981 – Kiwi Rupela Apr 22 '20 at 16:35
  • Will that also rename the branch on the remote? If not, how can one do that? – Michael Apr 29 '20 at 01:20
  • You will not lose changes (unstaged/modified files) when doing this locally. Say for example you are working on a new branch... then before you commit your changes, you realize what you actually did doesn't match the name of the branch. You can safely rename it before committing the changes. This way when it's merged, the names make sense later on. -- Just wanted to add that since it wasn't mentioned in case anyone else wonders what will happen :) – Wade Apr 14 '21 at 18:47
496
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."

Nick Matteo
  • 3,976
  • 20
  • 30
Madhan Ayyasamy
  • 13,442
  • 3
  • 16
  • 18
379

To rename your current branch:

git branch -m <newname>
Jonathan
  • 18,696
  • 6
  • 62
  • 66
336

Here are the steps to rename the branch:

  1. Switch to the 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 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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114
  • 2
    In which step would one unset the upstream? Before step 4? – Cyclonecode Jul 01 '18 at 07:06
  • 3
    This is the best answer here as it describes the full process to correctly complete a rename – Chris Halcrow Apr 21 '20 at 06:58
  • To explain the steps: 1 = switch to branch *locally*, 2 = 'move' i.e. 'rename' branch locally (`-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 remote – Chris Halcrow Jun 18 '20 at 23:48
  • 2
    @Milind Anantwar, what does it mean to "check that the new branch is pointing to it's own ref"? And could you please explain how `git branch --unset-upstream` resolves the unsynchronised condition(s) to which you're referring? – Chris Halcrow Jun 18 '20 at 23:52
  • I also used this `git push origin HEAD` Its working fine – AnilS Jul 06 '20 at 13:35
  • I also used this git push origin HEAD Its working fine – AnilS Jul 06 '20 at 13:37
  • Adding `--set-upstream` on step 4 should do the trick too. – Ph3n0x Oct 07 '20 at 13:00
252

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
trungk18
  • 18,147
  • 6
  • 41
  • 69
  • 1
    [This one](https://stackoverflow.com/a/29650705/1030960) worked better for me. Here the 2 steps gave me the following errors: `error: dst ref refs/heads/ receives from more than one src.; error: failed to push some refs to 'git@uri:foo/bar.git'` – Anto Feb 23 '18 at 15:26
  • 1
    You got the problem when running the command `git push origin :old-name new-name` right? – trungk18 Feb 23 '18 at 15:28
  • Yep exactly (sorry I meant "2nd step", not "2 steps" -- tired) – Anto Feb 23 '18 at 16:28
131

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.

mskfisher
  • 3,028
  • 3
  • 31
  • 47
Vanchev
  • 1,464
  • 1
  • 9
  • 7
  • 3
    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 – Vanchev Jun 26 '15 at 16:48
  • The `-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.`) – Jon Schneider Feb 14 '18 at 20:10
104

1. Rename

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

2. Track a new remote branch

- 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
pkamb
  • 26,648
  • 20
  • 124
  • 157
Oss
  • 4,052
  • 2
  • 18
  • 35
82

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
Samuel Meacham
  • 9,845
  • 7
  • 42
  • 50
  • 9
    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
  • 3
    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 to get the full hash if you need it. – House of Dexter Mar 06 '17 at 17:19
  • You can also use git show-ref this will give you the long hash of everything in your local repo. and I mean everything...branches/stashes/tags...etc – House of Dexter Mar 07 '17 at 19:40
77

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

Harry_pb
  • 4,892
  • 1
  • 30
  • 44
76

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]
aliasav
  • 2,615
  • 2
  • 21
  • 30
45

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hafiz Shehbaz Ali
  • 2,306
  • 21
  • 20
42

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
Tito Amoo
  • 293
  • 2
  • 14
Jethik
  • 1,734
  • 1
  • 21
  • 26
38
  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
  1. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

  1. Reset the upstream branch for the new-name local branch. Switch to the branch and then:

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

Major
  • 482
  • 5
  • 15
badarshahzad
  • 1,097
  • 14
  • 23
30

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hazarapet Tunanyan
  • 2,503
  • 23
  • 25
27

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
P4C
  • 477
  • 5
  • 10
  • 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
  • Alternatively, you can use `-M` instead of `-m` to do this kind of "casing fix" rename in a single step. – Jon Schneider Feb 14 '18 at 20:09
25

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.

Pierre-Olivier Vares
  • 1,113
  • 11
  • 17
24

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:

Change branch name locally

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alireza
  • 83,698
  • 19
  • 241
  • 152
22

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'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
dentuzhik
  • 702
  • 7
  • 16
20

If you are willing to use SourceTree (which I strongly recommend), you can right click your branch and chose 'Rename'.

enter image description here

Pang
  • 8,605
  • 144
  • 77
  • 113
Marcin Szymczak
  • 9,665
  • 4
  • 48
  • 60
19

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Steve Chambers
  • 31,993
  • 15
  • 129
  • 173
  • 6
    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 08 '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 08 '15 at 17:17
18

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Nomade
  • 1,673
  • 2
  • 18
  • 32
16

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Tanah
  • 349
  • 2
  • 8
14

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
nikkypx
  • 1,620
  • 16
  • 12
11

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
2Toad
  • 13,711
  • 7
  • 38
  • 38
Arif
  • 171
  • 1
  • 7
7

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:

  1. Rename local: just use the following command to rename your current branch, even you checked it out:
    git branch -m <old-branch-name> <new-branch-name>
    
  2. Delete the server one: use the following command to delete the old name branch on the server:
    git push <remote-name[origin by default]> :<old-branch-name>
    
  3. Push the new one: now it's time to push the new branch named on the server:
    git push -u <new-branch-name>
    
AmerllicA
  • 15,720
  • 11
  • 72
  • 103
  • 1
    in my case 3rd command is taking care to rename remote branch, without executing above 2 nd command. Is it necessary to delete before renaming remote branch? – SP007 Sep 17 '20 at 15:53
  • 1
    @SP007, The 2nd command is not essential, but I'm a little worry about clarity on git server. so I don't keep useless branches. – AmerllicA Sep 17 '20 at 18:52
  • when I execute 3rd command it renamed existing remote branch. – SP007 Sep 17 '20 at 19:42
  • 1
    @SP007, I will test it, maybe the 2nd command is not needed. – AmerllicA Sep 17 '20 at 20:05
6

Git branch rename can be done by using:

  1. git branch -m oldBranch newBranch

  2. 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$
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mohideen bin Mohammed
  • 14,471
  • 7
  • 86
  • 95
6

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:

  1. Using the Git rename branch command will require you to add an -m option to your command:

    git branch -m new-name
    
  2. 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
    
  3. 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:

  1. 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
    
  2. Reset the upstream branch for your new local branch and you will be all set:

    git push origin -u new-name
    
S. Hesam
  • 1,784
  • 1
  • 11
  • 33
5

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.

Rename a local Git branch

Ivan
  • 3,463
  • 28
  • 26
4

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.

Dai Kaixian
  • 877
  • 2
  • 12
  • 21
  • 1
    If you're having trouble remembering commands, you can set up shell or git aliases for yourself. – sean Dec 17 '19 at 07:46
3

If you want to:

  • Rename the Git repository, run: git branch -m <oldname> <newname>
  • Delete the old branch by: git push origin: old-name new-name
  • Commit it using: git commit <newname>
    • and then push using: git push origin new_branch_name:master
  • If you want to check the status then use: git status
  • If you want to check out then use: git checkout
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Vineet Jain
  • 1,288
  • 4
  • 16
  • 31
1

In PhpStorm:

VCS → Git → Branches... → Local Branches → _your_branch_ → Rename

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
AlexSi
  • 81
  • 1
  • 4
1

All you have to do are the three steps:

  1. Give the old branch under .git/refs/heads the new name
  2. Give the old branch under .git/logs/refs/heads the new name
  3. Update the .git/HEAD to lead to your new branch name
0

For Locally

at first change your current branch from the branch you want to update name for example I have 3 branch branch1 , branch2 , branch3

check current branch

git branch --show-current

output may : branch1

then you can update name of branch2 and branch3 not the current one

git branch -m old_branchname new_branchname



For remote

Just three steps to replicate change in name on remote as well as on GitHub:

git branch -m old_branchname new_branchname
git push origin :old_branchname new_branchname
git push --set-upstream origin new_branchname
MD SHAYON
  • 2,833
  • 21
  • 18
-3
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.

Saad Bilal
  • 1,681
  • 1
  • 15
  • 28