简体   繁体   English

在git中删除断开的分支

[英]Delete broken branch in git

I have created branch with "git checkout -b mybranch". 我用“git checkout -b mybranch”创建了分支。 It seems that something went wrong, and now I can't delete it using "git branch -D mybranch". 似乎出了点问题,现在我无法使用“git branch -D mybranch”删除它。 It says: error: branch 'mybranch' not found. 它说:错误:没有找到分支'mybranch'。

If git branch -D really isn't working, then your only option is to side-step it and edit the git check-out's state yourself. 如果git branch -D确实无法正常工作,那么你唯一的选择就是自己动手并编辑git check-out的状态。 You should go to the root-directory of your check-out (where the .git directory is) and 您应该转到签出的根目录( .git目录所在的位置)和

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

I used git update-ref -d refs/heads/<branch name> to fix this issue. 我使用git update-ref -d refs/heads/<branch name>来解决这个问题。 Presumably, this does the same thing as what Rup suggests in the selected answer except it's interfaced via Git's CLI. 据推测,这与Rup在所选答案中的建议完全相同,只不过它通过Git的CLI接口。

I have the same problem git branch -d <branch_name> was not working, And I didn't found anything in .git/packed-refs and .git/refs/heads but I got files in 我有同样的问题git branch -d <branch_name>无法正常工作,而且我在.git/packed-refs.git/refs/heads找不到任何内容但是我收到了文件

.git/refs/remotes/origin git的/参/遥控器/产地

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_name that I wanted to delete it was not showing in local. 但删除我想删除的branch_name文件后,它没有显示在本地。

To delete it on remote use 在远程使用时删除它

git fetch -p origin

The -p --prune option tells that before fetching, remove any remote-tracking references that no longer exist on the remote. -p --prune选项告知在获取之前,删除远程不再存在的任何远程跟踪引用。 Then use command 然后使用命令

git push origin :<branch_name_you_was_unable_to_delete>

to delete on remote. 删除远程。

And you are done. 你完成了。 :) :)

You obviously don't need to delete a branch that does not exist. 您显然不需要删除不存在的分支。 Use git branch to see a list of branches, if it's not in there, then there is no branch, and you don't need to delete it. 使用git branch查看git branch列表,如果它不在那里,那么就没有分支,你不需要删除它。 Otherwise make sure to type the name correctly and git branch -D should work. 否则请确保正确输入名称, 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字节文件,所以不需要担心。

I had the same problem. 我有同样的问题。 The branch was on the list of branches whenever I executed the git branch command, 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 (是的,它工作),然后我检查回主人和...再次执行git branch -D broken_branch

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM