如何删除远程 Git 存储库上的 HEAD 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24720975/
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
How to delete HEAD branch on remote Git repository?
提问by Pablo
This is remote Git repository on server
这是服务器上的远程 Git 存储库
[aaa@web48 proj.git]$ git ls-remote .
dfca707432eb53678b37026b160a4bdc7f1ac6c3 HEAD
dfca707432eb53678b37026b160a4bdc7f1ac6c3 refs/heads/master
1e09c37443ee758644a712e3c1a8b08b18a1f50d refs/heads/placeholder
I want to delete HEAD/master branch. How can I do it either on server or remotely? I'm using Tower client.
我想删除 HEAD/master 分支。如何在服务器上或远程执行此操作?我正在使用 Tower 客户端。
回答by
You cannot delete a remote branch if it's currently the default HEAD branch
如果远程分支当前是默认的 HEAD 分支,则无法删除它
The HEAD
symbolic reference on a remote bare repo represents the default branch for that repo. Any non-bare clones of that repo will automatically checkout that branch after the clone.
HEAD
远程裸仓库上的符号引用代表该仓库的默认分支。该 repo 的任何非裸克隆将在克隆后自动检出该分支。
Because it's the default, you can't just delete it like you normallywould, Git won't let you:
因为它是默认的,你不能像往常一样删除它,Git 不会让你:
$ git push origin --delete master
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error:
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error:
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To c:/Users/Keoki/Documents/GitHub/bare
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'c:/Users/Keoki/Documents/GitHub/bare'
The error message above points out that you can bypass the safety checks to delete the current HEAD
branch in the remote anyways, but I'm going to show you how to change what the default branch is, so that you can still keep a default branch, but delete master
like you wanted to.
上面的错误消息指出,HEAD
无论如何您都可以绕过安全检查来删除远程中的当前分支,但我将向您展示如何更改默认分支,以便您仍然可以保留默认分支,但删除master
你想要的。
Changing the default HEAD branch from the command line
从命令行更改默认 HEAD 分支
You can change what the default branch is in the remote repo if you have access to the remote. If you're using a hosting provider like GitHub or Bitbucket, they should allow you to change the default branch through their web interface.
如果您有权访问远程仓库,则可以更改远程仓库中的默认分支。如果您使用的是 GitHub 或 Bitbucket 等托管服务提供商,他们应该允许您通过其 Web 界面更改默认分支。
So if you have access to the remote, use the following command to change which branch the symbolic reference HEAD
points to:
因此,如果您有权访问远程,请使用以下命令更改符号引用HEAD
指向的分支:
git symbolic-ref HEAD refs/heads/<newDefaultBranch>
Changing the default HEAD branch on GitHub or Bitbucket
更改 GitHub 或 Bitbucket 上的默认 HEAD 分支
As I've already mentioned in the previous section, you can update the default HEAD
branch in your remote repo through the web interface if you use a hosting provide like GitHub or Bitbucket.
正如我在上一节中已经提到的,HEAD
如果您使用 GitHub 或 Bitbucket 等托管服务,则可以通过 Web 界面更新远程存储库中的默认分支。
GitHub
GitHub
Go to your repo's Settings tab, and you'll see the default branch setting right at the top,
转到您的 repo 的 Settings 选项卡,您将在顶部看到默认的分支设置,
Bitbucket
比特桶
Got to your repo's Settings tab, and you'll see the default branch setting near the middle,
进入你的 repo 的 Settings 选项卡,你会看到中间附近的默认分支设置,
Update your local clones' references to the default branch in the remote
更新本地克隆对远程默认分支的引用
Once you've updated the default branch in the remote bare repo, you'll need to update where your local clones of that repo think that the default HEAD
branch in the remote points to. You can do that with
更新远程裸仓库中的默认分支后,您需要更新该仓库的本地克隆认为HEAD
远程默认分支指向的位置。你可以这样做
git remote set-head <remote> --auto
# Or shorter
git remote set-head <remote> -a
You can confirm that the local repo has been properly updated using
您可以使用以下命令确认本地存储库已正确更新
$ git branch -r
origin/HEAD -> origin/foo
origin/foo
origin/master
Now you can delete the master branch on the remote
现在你可以删除远程的 master 分支了
Now that you've changed the default HEAD
branch on the remote to be something other than the master branch, you'll be able to delete it on the remote,
现在您已经HEAD
将远程上的默认分支更改为master 分支以外的其他分支,您将能够在远程上删除它,
$ git push origin --delete master
To c:/Users/Keoki/Documents/GitHub/bare
- [deleted] master
# Older syntax
$ git push origin :master
Additional References and Documentation
其他参考资料和文档
回答by VonC
Note: while you cannot indeed delete the default HEAD branch, you could, until Git 2.11 (Q4 2016) delete HEAD
itself!
注意:虽然您确实无法删除默认的 HEAD 分支,但您可以在 Git 2.11(2016 年第 4 季度)之前删除HEAD
自己!
symbolic-ref -d
: do not allow removal of HEAD
symbolic-ref -d
: 不允许移除 HEAD
See commit 12cfa79(02 Sep 2016) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
--in commit d1de693, 12 Sep 2016)
请参阅Junio C Hamano() 的commit 12cfa79(2016 年 9 月 2 日)。(由Junio C Hamano合并-- --在d1de693 提交中,2016 年 9 月 12 日)gitster
gitster
"
git symbolic-ref -d HEAD
" happily removes the symbolic ref, but the resulting repository becomes an invalid one.
Teach the command to forbid removal of HEAD.If you delete the symbolic-ref
HEAD
from a repository, Git no longer considers the repository valid, and even "git symbolic-ref HEAD refs/heads/master
" would not be able to recover from that state
(although "git init
" can, but that is a sure sign that you are talking about a "broken" repository).In the spirit similar to afe5d3d ("symbolic ref: refuse non-ref targets in HEAD", 2009-01-29), forbid removal of
HEAD
to avoid corrupting a repository.
"
git symbolic-ref -d HEAD
" 愉快地删除了符号引用,但生成的存储库变成了一个无效的存储库。
教命令禁止移除 HEAD。如果您删除符号-REF
HEAD
从档案库,Git会不再考虑存储库的有效,甚至连“git symbolic-ref HEAD refs/heads/master
”将无法从该状态中恢复
(虽然“git init
”可以,但这是一个明确的信号,你所谈论的是“损坏的”存储库)。本着类似于afe5d3d ("symbolic ref:reject non-ref targets in HEAD", 2009-01-29)的精神,禁止删除
HEAD
以避免损坏存储库。