git-remote set fetch address to mirror

Note that the push URL and the fetch URL, even though they can be set differently, must still refer to the same place. What you pushed to the push URL should be what you would see if you immediately fetched from the fetch URL. If you are trying to fetch from one place (e.g. your upstream) and push to another (e.g. your publishing repository), use two separate remotes.

虽然 文档 上这么说了,push 和 fetch 的地址需要指向同一个位置,但我想设置个镜像,从 GitHub Proxy 测速 拉取,是不是也能满足 ” 拉取刚推送 ” 的要求。

可没个独立命令能只改 fetch ,所以只能结合了。

PUSH_URL=$(git remote get-url origin --push)
git remote set-url origin $FETCH_URL
git remote set-url origin --push $PUSH_URL

如果仓库只会 fetch,可以一行替换到 mirror

git remote set-url origin "https://gh-proxy.com/$(git remote get-url origin --push)"

但注意非 GitHub 域名的地址,在 VSCode 中无法支持 ” 在 GitHub 中打开 ” 的 git blame 功能