47

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
  • 48,033
  • 60
  • 165
  • 247

5 Answers5

66

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,438
  • 1
  • 28
  • 41
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
  • 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,545
  • 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,741
  • 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
  • 246,190
  • 53
  • 318
  • 364
  • This may give an unwanted result: There might be something on `master` the OP wants to get rid of (if `experimental` is not rebased to the current tip of `master`). – Lutz Prechelt Aug 24 '22 at 14:13
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
  • 126,015
  • 21
  • 180
  • 199
  • This deletes the commit history of the deleted branch on remote. In many situations with a robust SDLC this is fine. The Ops question exposes that they had 2 unstable/dev branches that couldn't be merged (supposedly), one with a special name "master". The most popular answer keeps an intact history of commit histories of old and new master branches. – Tenacious Dec 01 '22 at 19:32