133

We would like to enforce a new policy for our projects that the master branch now be called the release branch to ensure it is more clear as to how the branch should be used. Naturally, we will have develop and release candidate branches as well.

I understand I can rename the master branch locally by simply using the following:

git branch -m master release

However, that is only locally. Even if I push this up to the remote, the HEAD still points to the remote master branch. I want to get rid of the master branch completely and make the default local branch upon initial clone, be release.

How can I achieve this?

It seems that since the origin is on a Gitorious server, I get errors deleting the master branch. I'm trying to see now if it is possible to change this so that the default branch is 'release'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kyle Hayes
  • 5,225
  • 8
  • 38
  • 53
  • 2
    Heh, fair enough. Long term worth is high enough to at least try. – Kyle Hayes Jan 06 '12 at 19:01
  • Possible duplicate of [How do I rename a local Git branch?](https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch) – Vineet Jain Aug 26 '17 at 16:11
  • Some (remote) server refuse to delete the "default" branch (it's the case with Github). So you perhaps need to go on the server to choose another "default" branch for the time of the operations... On Github, this can be done in the "branch" view. – jehon Jun 19 '19 at 15:55
  • 1
    `git branch` now supports a `--move` flag https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---move – alxndr Jun 23 '20 at 18:33

8 Answers8

157
git checkout -b release master    # Create and switch to the release branch
git push -u origin release        # Push the release branch to the remote and track it
git branch -d master              # Delete local master
git push --delete origin master   # Delete remote master
git remote prune origin           # Delete the remote tracking branch

Please note, if you are using GitHub you will need to first change your "default" branch on GitHub after step 3:

In your repository on github.com go SettingsBranchesDefault Branch. Change it to release and then do the rest of the steps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 4
    When I attempt git push --delete, I get the following error: deletion of the current branch prohibited – Kyle Hayes Jan 06 '12 at 18:56
  • 8
    You are using github or something similar, you need to make the default branch something else: http://matthew-brett.github.com/pydagogue/gh_delete_master.html or just leave master there and ignore it. – Adam Dymitruk Jan 06 '12 at 19:00
  • Yeah, we have a gitorious instance. Let me see if that is an option. – Kyle Hayes Jan 06 '12 at 19:18
  • It looks like gitorious' commit hooks forbid the deletion of the master branch -- for no good reason that I can see :/ – fge Jan 06 '12 at 19:24
  • bah! Well, then perhaps I will just have to ignore the master branch or simply use the name "master" and know that it is the release branch. I wonder if there is a way to at least tell gitorious to make the release branch a default if we went that route. I know we are eventually moving to an enterprise license for github, but not there yet. – Kyle Hayes Jan 06 '12 at 19:27
  • 1
    Same `! [remote rejected] branch (deletion of the current branch prohibited) ` will happen with Bitbucket. Switch the "Main Repository' in the settings screen (under the gear icon). – dnfehren Feb 27 '14 at 18:41
  • The remote (even if bare) has a `HEAD`, which is where the next `git clone` will start off. From that remote repository, change `HEAD` to the new branch via `git symbolic-ref HEAD refs/heads/release` Once you've move `HEAD` off of `master`, git won't be as bothered about deleting `master`. – bk. Apr 02 '16 at 19:02
  • May I ask _(because I want to start this right)_, I have a local repository, I haven't push it on GitHub yet (never been since I created it)... If I will to rename the master now all should I do is **git checkout -b release master** and **git branch -d master** and Im all done? – ailia Dec 10 '19 at 19:33
13

Check out your master branch

git checkout master

Create your release branch and switch to it:

git branch release
git checkout release

Push that to the server

git push origin release

Delete the master branch reference on the server

git push origin :master

Delete the local master branch

git branch -d master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
  • 2
    I can tell by these steps I will run into the same error as I mentioned above. – Kyle Hayes Jan 06 '12 at 19:00
  • @KyleHayes That's a configuration issue of the server. Though it is this way by default, the process to alter that should either be apparent to the user or [easily discoverable on Stackoverflow](http://stackoverflow.com/questions/2196874/cannot-delete-remote-branch-in-git) – Jeff Ferland Jan 06 '12 at 19:51
  • You have to switch to another branch before you can delete a branch. – martinedwards Jul 01 '20 at 10:07
6

Note: This answer is intended for self-hosted Git servers where you have command line access.

Since trying to delete the remote master from a client indeed is not allowed and I do assume forbidding denyDeleteCurrent makes sense, I would not like to change that setting.

However, I found that the easiest way to rename your master iff you have command line access to the remote server is to run the rename command directly on remote.

This worked for me:

  1. Login via SSH to the remote git server
  2. Go to the xxx.git folder of your project
  3. run: git branch -m master release

Now the remote repository uses release as its default branch and any git clone on that repository from any client will check out the release branch by default.

It is very helpful also after setting up a bare repository to configure it to your needs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christopher Lörken
  • 2,740
  • 18
  • 17
3

As previously stated by others, the issue here is Gitorious, which doesn't let you delete the HEAD branch per default. You have two options get around this problem. One is to log into the Gitorious server (with ssh), find the Git repository on the file server and add:

[receive]
        denyDeleteCurrent = warn

to the configuration.

An easier option is just to change the default branch. Go to you repository in the Gitorious web interface, press "Edit repository", and set "Head Change the symbolic ref the HEAD in the Git repository points to:". After you've done this you can delete the master branch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
2

If you run into this issue with GitHub, do the steps up until deleting the branch on remote. It will not let you do that. Then log into the Web interface and on the repository go SettingsBranchesDefault Branch. Change it to the new branch and do the rest of the steps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gru
  • 400
  • 2
  • 8
2

As of Git 2.28 (released 27th July 2020), you can now configure the name of the branch created when you init a new repository:

$ git config --global init.defaultBranch main

After setting this variable, running git init will produce a repository whose initial branch is main:

$ git init

Initialised empty Git repository in /home/thomas/test-git-repo/.git/ $ git status On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track) Release notes: https://lore.kernel.org/git/xmqq5za8hpir.fsf@gitster.c.googlers.com/

cc Kiley

1

Ideally, you want to set up tracking, so do this:

git push origin HEAD:release
git checkout --track origin/release

Now, do you want to delete the others?

git branch -d master
git push origin :master

Simple!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gahooa
  • 131,293
  • 12
  • 98
  • 101
  • I got the same error as the one I posted in @Adam's comment when I got to the git push origin :master command. – Kyle Hayes Jan 06 '12 at 18:58
0

Since you are done with renaming the branches, to set the HEAD to release for remote

git remote set-head origin release

Then to delete master branch in remote, you would have to be the administrator, at least on GitHub. Please refer to this post for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zyy
  • 1,271
  • 15
  • 25