通过删除 git 中的 /refs/heads/review/* 来删除分支是否安全?

Is it safe to delete branches by removing /refs/heads/review/* in git?

我正在使用 Gerrit 并且我经常通过 git review -d change-# 下载变更集。这留下了大量 review/user/topic 格式的分支。

通过 rm -rf .git/refs/heads/review 直接删除这些是否安全?我知道我可以从 Gerrit 取回提交,但我不想破坏本地存储库。

这取决于你"safe"的意思。

删除那些分支头文件不会破坏 Git 存储库。但是,如果一些审查分支已经"packed"(现在在.git/packed-refs),它也不会删除那些分支负责人。如果符合 "safe",那么是的,它是安全的。 :-)(它也不会删除这些引用的任何 reflog,这在某种程度上更糟,因为这些 reflog 可能会保护审查请求项目不被 GC。)

更好的方法是在每个 review/user/topic 上使用 git branch -D,这将删除引用 refs/heads/review/user/topic 和任何关联的 reflog,无论分支是否已打包。要枚举这些主题,请使用 git for-each-ref:

git for-each-ref --format='%(refname:short)' refs/heads/review | xargs git branch -D

例如