如何将我的 git 'master' 分支重命名为 'release'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8762601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:39:55  来源:igfitidea点击:

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

gitversion-controlbranchgit-branch

提问by Kyle Hayes

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.

我们希望为我们的项目实施一项新政策,即现在将 master 分支称为 release 分支,以确保更清楚应该如何使用该分支。当然,我们也会开发和发布候选分支。

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.

然而,这只是局部的。即使我将其推送到远程,HEAD 仍然指向远程主分支。我想完全摆脱 master 分支,并在初始克隆时创建默认的本地分支,即发布。

How can I achieve this?

我怎样才能做到这一点?

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

编辑:似乎由于源在一个巨大的服务器上,我在删除主分支时出错。我现在正在尝试查看是否可以更改此设置,以便默认分支为“发布”。

回答by Adam Dymitruk

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

回答by Jeff Ferland

Checkout 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

删除服务器上的master分支引用

git push origin :master

Delete the local master branch

删除本地master分支

git branch -d master

回答by Aleksander Blomsk?ld

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:

正如其他人之前所说,这里的问题是 Gitorious,它不允许您默认删除 HEAD 分支。您有两种选择可以解决这个问题。一种是登录gitorious服务器(用ssh),在文件服务器上找到git-repository,添加:

[receive]
        denyDeleteCurrent = warn

to the config.

到配置。

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.

一个更简单的选择是更改默认分支。在gitorious Web 界面中转到您的存储库,按“编辑存储库”,然后设置“将git 存储库中的HEAD 指向的符号引用更改为:”。完成此操作后,您可以删除 master 分支。

回答by Christopher L?rken

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

注意:此答案适用于您具有命令行访问权限的自托管 git 服务器。

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

由于remote master确实不允许尝试从客户端删除并且我确实认为禁止denyDeleteCurrent是有道理的,因此我不想更改该设置。

However, I found that the easiest way to rename your master iff you have command line access to the remote serveris 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
  1. 通过SSH登录远程git服务器
  2. 转到项目的 xxx.git 文件夹
  3. 跑: git branch -m master release

Now the remote repository uses releaseas it's default branch and any git cloneon that repository from any client will check out the release branch by default.

现在远程存储库release用作它的默认分支,并且git clone来自任何客户端的存储库上的任何内容都将默认检出发布分支。

Very helpful also after setting up a bare repository to configure it to your needs.

在设置裸存储库以根据您的需要对其进行配置后也非常有帮助。

回答by gahooa

Ideally, you want to setup tracking, so do this:

理想情况下,您想设置跟踪,请执行以下操作:

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

Now, you want to delete the others?

现在,您要删除其他人吗?

git branch -d master
git push origin :master

Simple!

简单的!

回答by Gru

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 repo go Settings > Branches > Default Branch. Change it to the new branch and do the rest of the steps.

如果您在使用GitHub 时遇到此问题,请执行上述步骤,直到删除远程分支。它不会让你这样做。然后登录 Web 界面并在 repo 上转到 Settings > Branches > Default Branch。将其更改为新分支并执行其余步骤。