Git 拒绝删除分支

Yar*_*Yar 5 git github

我在这个 GitHub 存储库上有一个分支(不是 master),现在我尝试重命名它,由于某种原因,我收到此错误:

git push origin :OldName
To git@github.com:user/gitRepo.git
 ! [remote rejected] OldName (refusing to delete the current branch: refs/heads/OldName)
error: failed to push some refs to 'git@github.com:user/gitRepo.git'
Run Code Online (Sandbox Code Playgroud)

这是因为我没有任何主分支吗?这是我的git branch -a

OldName
remotes/origin/HEAD -> origin/OldName
remotes/origin/OldName
Run Code Online (Sandbox Code Playgroud)

sta*_*cke 6

这不是 [git] 限制,而是 [github] 为您提供额外的理智保护。更改您的 GitHub 项目中的[默认分支],然后重试。

GitHub 上设置页面的示例 URL(更改[user][project] ):

https://github.com/[user]/[project]/settings/branches
                   ^      ^
                   change these placeholders
Run Code Online (Sandbox Code Playgroud)

重申一下:

  1. 调整上面的 URL 以到达/settings/branches您自己项目的页面。
  2. 进行任何必要的更改以有效地“取消保护”相关分支。
  3. 再次重试“推送删除”操作,它应该可以工作。


Tim*_*sen 4

如果输出git branch -a准确,那么看起来您只有一个分支OldName,并且在 GitHub 上只有一个同名的相应分支。删除存储库中的唯一分支对您来说没有多大意义,因为那样的话该存储库中就没有内容,什么也没有(除了引用日志,如果它仍然存在的话)。

具体来说,该错误可能是由于略有不同的原因引起的。由于该OldName分支是 GitHub 中的唯一分支,因此也很可能是存储库的默认分支。GitHub 不允许您删除默认分支,因为这意味着浏览您的存储库的人可能无法看到内容。

要解决此问题,您可以使用所需名称创建一个分支,然后从那里进行删除:

本地

git checkout -b NewName  # create a dummy branch from OldName
git push origin NewName
Run Code Online (Sandbox Code Playgroud)

在 GitHub 上

将默认分支更改为NewName

本地(再次):

git push origin :OldName
Run Code Online (Sandbox Code Playgroud)