git 无法删除无意创建的远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2895906/
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-19 04:18:27  来源:igfitidea点击:

Cannot delete a remote branch created unintentionally

gitversion-control

提问by Himel

$ git branch -a
* SocialAct
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/SocialAct
  remotes/origin/social

I want to delete the remote branch "remotes/origin/social", and applied folloing command:

我想删除远程分支“remotes/origin/social”,并应用以下命令:

$ git branch -d -r origin/social
Deleted remote branch origin/social (was 26f6f61).

But I have no idea how to bring these changes remotely so that the branches are deleted from origin and everyone can see the changes. I tried git push but that does not work

但是我不知道如何远程进行这些更改,以便从源中删除分支并且每个人都可以看到更改。我试过 git push 但这不起作用

Any help.

任何帮助。

回答by Nathan McDaniel

I had this error (from above):

我有这个错误(从上面):

Thanks. Actually I noticed this solution and tried earlier. But this gives following error... $ git push origin :heads/socail Enter passphrase for key '/h/.ssh/id_rsa': error: unable to push to unqualified destination: heads/socail The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to '[email protected]' – Himel May 24 '10 at 9:37

谢谢。实际上我注意到了这个解决方案并更早地尝试过。但这会导致以下错误... $ git push origin :heads/socail Enter passphrase for key '/h/.ssh/id_rsa': 错误:无法推送到不合格的目的地:heads/socail 目的地 refspec 既不匹配现有的 ref在遥控器上也不以 refs/ 开头,我们无法根据源 ref 猜测前缀。错误:未能将一些引用推送到“[email protected]” – Himel 10 年 5 月 24 日 9:37

It seemed to have gotten confused about whether or not I had actually deleted it remotely. I worked around it like so:

似乎对我是否真的远程删除了它感到困惑。我像这样解决它:

git push origin HEAD:branch_to_delete

git push origin :branch_to_delete

That worked for me. Using: git version 1.7.3.1.msysgit.0.

那对我有用。使用:git 版本 1.7.3.1.msysgit.0

回答by 0b10011

As mentioned by @Joshin a comment on Nathan McDaniel's Answer, this is likely due to the branch no longer existing in the remote repository. This causes git branch -ato still show the branch under origin(or whatever you happened to name this particular remote), but deleting the branch on the remote repository is impossible because it no longer exists on the remote. This could have been caused by deleting the branch on the remote from another computer (on top of the fact that git pulland git fetchdo not remove references to remote branches that have been deleted from the remote repository).

正如@Josh在对Nathan McDaniel's Answer的评论中提到,这可能是由于远程存储库中不再存在该分支。这会导致git branch -a仍然显示分支origin(或您碰巧命名此特定远程的任何内容),但无法删除远程存储库上的分支,因为它不再存在于远程存储库中。这可能是由于从另一台计算机上删除远程分支引起的(事实上,git pull并且git fetch不删除对已从远程存储库中删除的远程分支的引用)。

The fix

修复

Simply remove all remote-tracking branches that have already been removed from the remote repository with git remote prune:

只需使用以下命令删除已从远程存储库中删除的所有远程跟踪分支git remote prune

git remote prune REMOTENAME

For example, if your remote's name is origin(likely), the above command would look like:

例如,如果您的遥控器的名称是origin(可能),则上述命令将如下所示:

git remote prune origin

From the documentation provided with git:

从提供的文档中git

git remote prune [-n | --dry-run] <name>

Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/".

With --dry-runoption, report what branches will be pruned, but do not actually prune them.

git remote prune [-n | --dry-run] <name>

删除 下所有陈旧的远程跟踪分支<name>。这些陈旧的分支已经从 引用的远程存储库中删除<name>,但在“remotes/”中仍然可以在本地使用。

使用--dry-run选项,报告将修剪哪些分支,但实际上并不修剪它们。

回答by Arkaitz Jimenez

git push origin :social
But you need to delete locally as well, before or after.

git push origin :social
但是您也需要在本地删除之前或之后。

回答by abhinav

The below command will delete the remote tracking branch but not the branch which exists on remote

下面的命令将删除远程跟踪分支,但不会删除远程上存在的分支

$ git branch -d -r origin/social
Deleted remote branch origin/social (was 26f6f61).

To delete remote branch:

删除远程分支:

git push origin  :social

This will automatically delete the remote tracking branch i.e remotes/origin/social.

这将自动删除远程跟踪分支,即 remotes/origin/social。

回答by Noufal Ibrahim

Deleting remote branches is described in detail over here.

此处详细描述了删除远程分支。