如何修复存在无效远程fetchref的“损坏”Git仓库?
修复Git远程仓库无效fetchRef导致的“损坏”状态
当Git远程仓库配置了无效fetchRef(即该Ref在远程仓库中不存在)时,这个远程仓库会进入“损坏”状态,无法执行任何相关操作。
测试示例
$ git config --local --add remote.origin.fetch "+refs/heads/foo/bar:refs/remotes/forks/foo/bar" $ git fetch fatal: couldn't find remote ref refs/heads/foo/bar fatal: the remote end hung up unexpectedly
当前使用的粗糙修复方法
以下是我目前用的修复方式,但感觉非常“粗糙”:
# A bad ref here will really mess up git remote=${1:-origin} for fetchref in $(git fetch "$remote" 2>&1 | grep "find remote ref" | sed -e "s/.*find remote ref//") do echo "Removing bad ref: $fetchref" git config --local --unset "remote.$remote.fetch" "$fetchref" done
相对改进但仍复杂脆弱的方法
下面这个方法稍好一些,但还是过于复杂且脆弱:
for fetchref in $(git config --get-all "remote.${remote}.fetch"); do ref="${fetchref#:*}" # everything after the colon [[ "$ref" =~ \*$ ]] && continue if ! git ls-remote --exit-code "${remote}" "$ref" &>/dev/null; then echo "Bad ref: $fetchref does not exist on ${remote}. Removing." git config --local --unset "remote.${remote}.fetch" "$ref" fi done
请问是否有更简洁可靠的方法来修复这类问题?
内容的提问来源于stack exchange,提问作者Mort
相关产品推荐
相关产品推荐

