git 删除远程分支不工作:找不到分支

git delete remote branch not working: branch not found

我正在尝试删除 git 中的一个远程分支,我做了:

git branch -r
...
origin/master
origin/dev
origin/branch_to_delete

现在我尝试删除 origin/branch_to_delete

git branch -d origin/branch_to_delete
error: branch 'origin/branch_to_delete' not found 

我做到了:

git fetch --all

又试了一遍,还是一样的错误。我试过 -D 但同样的错误。

但是分支就在那里,我可以在 github.com 中看到它。怎么办?

根据this post

Deleting is also a pretty simple task (despite it feeling a bit kludgy):

git push origin :newfeature

That will delete the newfeature branch on the origin remote, but you’ll still need to delete the branch locally with git branch -d newfeature.

所以你得到的错误只是意味着你没有那个分支的本地副本,所以你可以忽略它。然后删除远程副本:

git push origin :branch_to_delete

删除远程分支,命令为:

$ git push origin --delete [branch]

似乎有人忘记了先前答案之一中的“--delete”。

如果你在服务器上看不到它(这是我的情况,因为有人已经删除了远程服务器上的那个分支)但它仍然作为远程分支存在于你的本地 .git 存储库中,您将能够用 nor git push origin --delete [branch]git push origin :branch_to_delete 来解决问题。相反 运行

git branch --all
git remote prune origin
git branch --all

用您的遥控器名称替换 origin