git 删除远程git分支时“错误:无法推送到不合格的目的地”

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

When deleting remote git branch "error: unable to push to unqualified destination"

git

提问by Hugo

I'm trying to delete a remote git branch with

我正在尝试删除远程 git 分支

git push origin :my_remote_branch

and getting:

并获得:

error: unable to push to unqualified destination: my_remote_branch
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]:/myrepo'

these are my current branches

这些是我目前的分支

git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/my_remote_branch

git branch -r --merged
  origin/HEAD -> origin/master
  origin/develop
  origin/master

Any ideas on how I can get rid of this branch would be appreciated.

关于如何摆脱这个分支的任何想法将不胜感激。

回答by Jan Hudec

The fact that refs/remotes/origin/my_remote_branchexists in your local repository does not imply refs/heads/my_remote_branchexists in the originremote repository.

这样的事实refs/remotes/origin/my_remote_branch存在于你的本地库并不意味着refs/heads/my_remote_branch存在于origin远程仓库。

Do git fetch -p originto make refs/remotes/origin/my_remote_branchgo away if it's already deleted in origin. The -poption tells fetch to delete any tracking branches that no longer exist in the corresponding remotes; by default they are kept around.

如果它已经在原点被删除,请不要git fetch -p origin让它refs/remotes/origin/my_remote_branch消失。该-p选项告诉 fetch 删除相应遥控器中不再存在的任何跟踪分支;默认情况下,它们被保留。

回答by Hugo

Found question cleaning up old remote git branchesand this did the trick

发现问题清理旧的远程 git 分支,这做到了

git branch -r -d origin/my_remote_branch

回答by Matthew

I ran across this when trying to delete a remote branch that had already been deleted. All that was needed was a prune:

我在尝试删除已删除的远程分支时遇到了这个问题。所需要的只是修剪:

git remote prune origin

回答by Pawan Maheshwari

Try following two options to delete remote branch forcibly

尝试以下两个选项强制删除远程分支

Option 1

选项1

get push origin --delete <branchName>

Option 2

选项 2

git fetch -p origin
git branch -r -d origin/<branchName>

回答by pevik

git branch -r -d origin/my_remote_branch

was not enough for me. Before I had to go to server and work with git directory directly (which is dangerous and ugly) to remove the branch:

对我来说还不够。在我不得不去服务器并直接使用 git 目录(这是危险和丑陋的)来删除分支之前:

ssh mygitserver
su - git
cd /home/git/repositories/my_remote_branch.git/
git  --git-dir=. --work-tree=/tmp/ branch -D my_remote_branch

回答by Vlad Pulichev

For me the problem was, that this was my default branch on github. I changed default branch, then delete operation was succeeded.

对我来说,问题是,这是我在 github 上的默认分支。我更改了默认分支,然后删除操作成功。

Hope it helps to someone

希望对某人有帮助

回答by Kangqiao Zhao

I have the similar problem. First went to this discussion, however I couldn't solve the problem until I saw https://stackoverflow.com/a/32147743/4209849.

我有类似的问题。首先进入了这个讨论,但是直到我看到https://stackoverflow.com/a/32147743/4209849才解决问题。

which simply add a tip on distinguishing origin/my-branch-nameand my-branch-name.

这只是添加了区分origin/my-branch-name和的提示my-branch-name

To be specific, I should use:

具体来说,我应该使用:

git push origin :my_remote_branch

instead of

代替

git push origin :origin/my_remote_branch

This solved my problem at least, hope it would help others as well.

这至少解决了我的问题,希望它也能帮助其他人。

回答by Nutan

This worked for me: I created the remote branch on github UI and then pushed my local branch which had the same name to it. Try it in case other ways dont work. Other way would be creating a new branch locally and pushing an empty branch and later cherry-pick your commit and push again to your remote.

这对我有用:我在 github UI 上创建了远程分支,然后将具有相同名称的本地分支推送到它。试试吧,以防其他方法不起作用。另一种方法是在本地创建一个新分支并推送一个空分支,然后挑选您的提交并再次推送到您的远程。

回答by Kaweesi Joseph

Had this same issue, I manually edited my ./.git/configfile to include:

有同样的问题,我手动编辑了我的./.git/config文件以包括:

[branch "branchName"]
remote = origin
merge = refs/heads/branchName

Which resulted into: error: src refspec branchName matches more than one.This I fixed by running $git tag -d branchName. After which I was able to push the new branch to upstream.

这导致:error: src refspec branchName matches more than one.我通过运行$git tag -d branchName. 之后我能够将新分支推送到上游。