127

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

4
153
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.

8
  • 4
    When I attempt git push --delete, I get the following error: deletion of the current branch prohibited
    – Kyle Hayes
    Jan 6, 2012 at 18:56
  • 8
    You are using github or something similar, you need to make the default branch something else: matthew-brett.github.com/pydagogue/gh_delete_master.html or just leave master there and ignore it. Jan 6, 2012 at 19:00
  • Yeah, we have a gitorious instance. Let me see if that is an option.
    – Kyle Hayes
    Jan 6, 2012 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 6, 2012 at 19:24
  • 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, 2014 at 18:41
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
3
  • 2
    I can tell by these steps I will run into the same error as I mentioned above.
    – Kyle Hayes
    Jan 6, 2012 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 Jan 6, 2012 at 19:51
  • You have to switch to another branch before you can delete a branch. Jul 1, 2020 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.

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.

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.

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!

1
  • 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 6, 2012 at 18:58
1

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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