How do I rename my Git 'master' branch to 'release'?

GitVersion ControlBranchGit Branch

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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!

Solution 7 - Git

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/[email protected]/

cc Kiley

Solution 8 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKyle HayesView Question on Stackoverflow
Solution 1 - GitAdam DymitrukView Answer on Stackoverflow
Solution 2 - GitJeff FerlandView Answer on Stackoverflow
Solution 3 - GitChristopher LörkenView Answer on Stackoverflow
Solution 4 - GitAleksander BlomskøldView Answer on Stackoverflow
Solution 5 - GitGruView Answer on Stackoverflow
Solution 6 - GitgahooaView Answer on Stackoverflow
Solution 7 - GitLuigi RatkeView Answer on Stackoverflow
Solution 8 - GitzyyView Answer on Stackoverflow