git:如何删除本地引用分支?

51

我在删除本地引用分支(refs/notes/origin/commits)方面遇到了一些困难。 使用命令,我能够删除远程仓库中的该分支(refs/notes/origin/commits)。

git push origin :refs/notes/origin/commits

但是当我尝试在本地仓库删除相同的分支时,我会收到以下错误:

[User@centos Clone]# git branch -rd refs/notes/origin/commits
error: remote branch 'refs/notes/origin/commits' not found.

有任何建议吗?

3个回答

110

只需做

git branch -d commits

删除本地分支。

使用-D选项,无论是否合并,都可以将其删除。

使用

git update-ref -d refs/notes/origin/commits

删除引用。

您还可以按其他答案中所述进行硬删除。

rm -rf .git/refs/notes

4
这也有助于修复错误信息“error: cannot lock ref 'refs/remotes/origin/feature-x': is at [Guid1] but expected [Guid2]”。 - Jason L.
这非常适合于移除我本地创建的一个标签不正确的提交。这些提交在refs/tags/中。 - Jason R Stevens CFA
在你的建议中, git update-ref -d refs/notes/origin/commits 就是我正在寻找的。 - stanm
硬删除对我没有起作用,但是 git update-ref -d 起了作用。谢谢 :) - Alex White
我需要在本地删除一个错误的远程分支引用(git branch -r分支)。我的分支前缀略有不同。我需要运行 git update-ref -d refs/remotes/<Remote-Branch-Name>,其中在我的情况下 <Remote-Branch-Name>origin/feature/page-visit - Llama D'Attore

7

您的git仓库中有笔记,您可以通过以下方式删除一条笔记:

git notes remove <commit>

如果要删除所有笔记,您需要删除“notes”目录。

rm -rf .git/refs/notes

或者您可以使用git update-ref命令。
git update-ref -d refs/notes/commits

2
使用rm -rf <dir>相比于update-ref有什么优势?你是否有时需要使用rm -rf是因为无法使用后一条命令? - Guildenstern
1
不,我不认为会有一种情况下无法使用git命令git update-ref而且你应该始终使用它。这个git命令还会做一些其他的事情,比如更新像.git/packed-refs文件这样的"管理数据"。(请阅读manpage以了解这个命令/文件的作用)。我正在调整我的回答,以指出不应该使用rm命令。 - undefined

1

您可以直接从.git目录中删除该文件。从存储库根目录,使用以下命令即可:

rm .git/refs/tags/refs/original/refs/heads/master

如果git-tag命令失败,则路径可能略有不同,因此您可能需要进入.git/refs并通过试错找到有问题的head。删除该文件将从您的本地存储库中删除该引用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接