如何重命名我的 Git “master”分支到“发布”?
我们希望为我们的项目强制执行一项新政策,即主分支现在称为发布分支,以确保更清楚如何使用该分支。当然,我们也会开发和发布候选分支。
我知道我可以通过简单地使用以下命令在本地重命名主分支:
git branch -m master release
但是,这只是本地的。即使我将其推送到远程,HEAD 仍然指向远程主分支。我想完全摆脱主分支,并在初始克隆时创建默认本地分支,发布。
我怎样才能实现这个目标?
看来,由于源位于 Gitorious 服务器上,我在删除 master 分支时遇到错误。我现在正在尝试查看是否可以更改此设置,以便默认分支为“release”。
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'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请注意,如果您使用 GitHub,则需要在第 3 步之后首先更改 GitHub 上的“默认”分支:
在 github.com 上的存储库中,转到设置 → < em>分支 → 默认分支。将其更改为release,然后执行其余步骤。
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 Settings → Branches → Default Branch. Change it to release and then do the rest of the steps.
检查您的 master 分支
创建您的发布分支并切换到它:
将其推送到服务器
删除服务器上的 master 分支引用
删除本地 master 分支
Check out your master branch
Create your release branch and switch to it:
Push that to the server
Delete the master branch reference on the server
Delete the local master branch
注意:此答案适用于具有命令行访问权限的自托管 Git 服务器。
因为尝试从客户端删除
远程主机
确实是不允许的,但我确实这样做假设禁止denyDeleteCurrent
是有道理的,我不想更改该设置。但是,我发现重命名主服务器的最简单方法如果您可以通过命令行访问远程服务器是直接在远程运行重命名命令。
这对我有用:
gitbranch -m masterrelease
现在远程存储库使用
release
> 作为其默认分支,并且来自任何客户端的该存储库上的任何 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 forbiddingdenyDeleteCurrent
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:
git branch -m master release
Now the remote repository uses
release
as its default branch and anygit 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.
正如其他人之前所说,这里的问题是 Gitorious,它不允许您默认删除 HEAD 分支。您有两种选择来解决这个问题。一种是登录 Gitorious 服务器(使用 ssh),在文件服务器上找到 Git 存储库并添加:
到配置中。
一个更简单的选择是更改默认分支。转到 Gitorious Web 界面中的存储库,按“编辑存储库”,然后设置“Head 更改 Git 存储库中 HEAD 指向的符号引用:”。完成此操作后,您可以删除 master 分支。
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:
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.
如果您在使用 GitHub 时遇到此问题,请执行上述步骤,直到删除远程分支。它不会让你这样做。然后登录到 Web 界面并在存储库上转到设置 → 分支 → 默认分支。将其更改为新分支并执行其余步骤。
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 Settings → Branches → Default Branch. Change it to the new branch and do the rest of the steps.
从 Git 2.28(2020 年 7 月 27 日发布)开始,您现在可以配置在初始化新存储库时创建的分支的名称:
设置此变量后,运行 git init 将生成一个初始分支为 main 的存储库:
初始化空 Git 存储库在/home/thomas/test-git-repo/.git/
$ git 状态
在主分支上
没有提交,但
没有任何要提交的内容(创建/复制文件并使用“git add”来跟踪)
发行说明:https://lore.kernel.org/ 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:
After setting this variable, running git init will produce a repository whose initial branch is main:
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
理想情况下,您想要设置跟踪,因此请执行以下操作:
现在,您想删除其他吗?
简单的!
Ideally, you want to set up tracking, so do this:
Now, do you want to delete the others?
Simple!
既然您已经完成了分支的重命名,请将 HEAD 设置为远程的
release
然后要删除远程中的
master
分支,您必须是管理员,至少在GitHub。请参阅这篇文章 了解更多信息。Since you are done with renaming the branches, to set the HEAD to
release
for remoteThen 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.