在 git 中删除损坏的分支

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

Delete broken branch in git

git

提问by User Created Image

I have created branch with "git checkout -b mybranch". It seems that something went wrong, and now I can't delete it using "git branch -D mybranch". It says: error: branch 'mybranch' not found.

我用“git checkout -b mybranch”创建了分支。似乎出了点问题,现在我无法使用“git branch -D mybranch”删除它。它说:错误:找不到分支“mybranch”。

回答by Rup

If git branch -Dreally isn't working, then your only option is to side-step it and edit the git check-out's state yourself. You should go to the root-directory of your check-out (where the .gitdirectory is) and

如果git branch -D确实不起作用,那么您唯一的选择是回避它并自己编辑 git check-out 的状态。您应该转到结帐的根目录(.git目录所在的位置)和

  1. edit .git/packed-refs; if you see a line with your branch name then delete it
  2. look in .git/refs/headsfor a file named after your branch; if you see one, delete it
  1. 编辑.git/packed-refs; 如果您看到一行包含您的分支名称,则将其删除
  2. 查找.git/refs/heads以您的分支命名的文件;如果你看到一个,删除它

回答by solstice333

I used git update-ref -d refs/heads/<branch name>to fix this issue. Presumably, this does the same thing as what Rup suggests in the selected answer except it's interfaced via Git's CLI.

我曾经git update-ref -d refs/heads/<branch name>修复过这个问题。据推测,这与 Rup 在所选答案中建议的内容相同,只是它是通过 Git 的 CLI 连接的。

回答by GauRang Omar

I have the same problem git branch -d <branch_name>was not working, And I didn't found anything in .git/packed-refsand .git/refs/headsbut I got files in

我有同样的问题git branch -d <branch_name>没有工作,我没有找到任何东西.git/packed-refs.git/refs/heads但我得到了文件

.git/refs/remotes/origin

.git/refs/remotes/origin

with the name of the branches that I was not able to delete locally as well as in remote. But after deleting the files with the branch_namethat I wanted to delete it was not showing in local.

使用我无法在本地和远程删除的分支的名称。但是在删除带有branch_name我想删除的文件后,它没有显示在本地。

To delete it on remote use

在远程使用时删除它

git fetch -p origin

git fetch -p origin

The -p --pruneoption tells that before fetching, remove any remote-tracking references that no longer exist on the remote. Then use command

-p--prune选项告诉在获取之前,删除远程上不再存在的任何远程跟踪引用。然后使用命令

git push origin :<branch_name_you_was_unable_to_delete>

git push origin :<branch_name_you_was_unable_to_delete>

to delete on remote.

远程删除。

And you are done. :)

你已经完成了。:)

回答by poke

You obviously don't need to delete a branch that does not exist. Use git branchto see a list of branches, if it's not in there, then there is no branch, and you don't need to delete it. Otherwise make sure to type the name correctly and git branch -Dshould work.

您显然不需要删除不存在的分支。用git branch看分支的列表,如果它不在那里,那么就没有分支,你并不需要将其删除。否则请确保正确键入名称并且git branch -D应该可以正常工作。

Nevertheless you don't need to care much about a broken branch that might be still around but is inaccessible. Branches in Git are in fact simple 40 bytes files, so not really something you need to worry about.

尽管如此,您不需要太在意可能仍然存在但无法访问的损坏分支。Git 中的分支实际上是简单的 40 字节文件,因此您无需担心。

回答by ?ukasz Dumiszewski

I had the same problem. The branch was on the list of branches whenever I executed the git branchcommand, but I couldn't delete it.

我有同样的问题。每当我执行git branch命令时,分支都在分支列表中,但我无法删除它。

The solution in my case was simple and a bit unexpected: I checked out the broken branch git checkout broken_branch(yes, it worked), then I checked out back to master and... again executed git branch -D broken_branch.

在我的情况下,解决方案很简单,但有点出乎意料:我检查了损坏的分支git checkout broken_branch(是的,它有效),然后我检查回 master 并...再次执行git branch -D broken_branch

回答by Winand

If branch name contains special characters it needs to be quoted:

如果分支名称包含特殊字符,则需要用引号引起来:

$ git branch -D 'ENH-Adding-unit-``julian``-to-``to_datetime``'