42

I have 2 git branches: master and experimental.

Experimental became good, I want to make it the master. I figured I would rename to shuffle things around, but here is what I got:

nutebook:Stuff nathan$ git remote rename master old
error: Could not rename config section 'remote.master' to 'remote.old'

I use GitHub and Git-Tower.

Nathan H
  • 46,253
  • 59
  • 160
  • 244

5 Answers5

62

The following is a guide to rename your master branch. It will work just as easily to rename your experimental branch.

Here's how I did the renaming.

First, in your working tree, locally rename master to something else.

git branch -m master old-dev

Renaming a branch does work while you are on the branch, so there's no need to checkout something else.

Then, locally rename the maintenance branch (2.63-branch) to master:

git branch -m 2.63-branch master

Now, time to mess with the remote. Just in case you screw up, you might want to make sure you have a current backup. First, delete the remote's master:

git push origin :master

And now, give the remote your new master:

git push origin master:refs/heads/master

Update: When creating a new branch, the refs/heads/ prefix is needed on the remote side. If the branch already exists (as master did above) only the branch name is required on the remote side.

... and your now-renamed old master:

git push origin old-dev:refs/heads/old-dev

Finally, delete the old name of your maintenance branch to prevent confusion:

git push origin :2.63-branch

Clients will now get the 'new' master branch when they pull.

see this site.

Kim Gräsman
  • 7,268
  • 1
  • 27
  • 40
djhaskin987
  • 9,195
  • 4
  • 48
  • 82
  • 7
    To add to this: when I wanted to rename my 'master' on github, I first had to change the default branch under 'settings' before it would let me delete it (i.e. `git push origin :master`). – dave mankoff Feb 09 '13 at 22:47
  • Can you just rename the branch locally and push it to remote and then delete the old one? – Robert Beltran Feb 12 '14 at 00:01
  • 1
    I find the -u flag to push is nice because it sets the upstream tracking of the new remote up for you, e.g. `git push -u origin master` (using git 1.7 I didn't need the full refs/heads prefix) – Tom Saleeba Mar 25 '14 at 05:48
4

I think the easiest way is to checkout the experimental branch, delete the remote master branch, then push the local experimental one as the new remote master one.

// delete the remote master branch by pushing null
// (the space in front of the semicolon) in this branch
git push origin :master
// push local experimental to remote master
git push origin experimental:master
theor
  • 1,510
  • 1
  • 9
  • 16
1

If you're a Mac user, you can use the GitHub Mac App (https://mac.github.com/) to rename branches.

Jacek Lampart
  • 1,671
  • 13
  • 25
1

master is a branch, not a remote like origin is. if you want to have your experimental work to your master branch, simply merge it in:

git checkout master
git merge experimental
knittl
  • 216,605
  • 51
  • 293
  • 340
0

What you tried to do was rename a remote repo from "master" to "old". To rename a branch on another repo, just delete it with

git push <remote> :<branch name>

then push it as something else.

Ryan Stewart
  • 120,981
  • 20
  • 172
  • 194