一、为什么要换源?

在国内开发,访问 GitHub 经常遇到这些痛苦:

  • git clone 跑一半断了,重试 N 次
  • git push 卡住不动,喝杯咖啡回来还是 0%
  • CI/CD 拉代码超时,每天被运维追着问

根本原因:DNS 解析慢、国际带宽拥堵、偶尔被墙。换源就是换一条更快的路——可以是镜像站、SSH 协议、或加代理。

二、基础操作:查看和修改远程仓库

2.1 查看当前远程源

# 查看所有远程仓库名称和地址
git remote -v

# 输出示例:
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

2.2 修改远程源 URL

# 改 origin 的地址
git remote set-url origin <新地址>

# 示例:从 HTTPS 改成 SSH
git remote set-url origin git@github.com:user/repo.git

# 再确认一下
git remote -v

2.3 添加/删除远程源

# 添加一个叫 gitee 的备用源
git remote add gitee https://gitee.com/user/repo.git

# 删除远程源
git remote remove gitee

# 重命名远程源
git remote rename origin upstream

三、场景一:HTTPS ↔ SSH 互切

HTTPS → SSH(推荐,免密 + 更快)

# 1. 先生成 SSH Key(如果没有的话)
ssh-keygen -t ed25519 -C "your_email@example.com"

# 2. 复制公钥
cat ~/.ssh/id_ed25519.pub

# 3. 粘贴到 GitHub → Settings → SSH and GPG keys → New SSH Key

然后换源:

git remote set-url origin git@github.com:用户名/仓库名.git

# 测试连接
ssh -T git@github.com
# 看到 "Hi xxx! You've successfully authenticated" 就对了

SSH → HTTPS

git remote set-url origin https://github.com/用户名/仓库名.git

# 设置记住密码(避免每次输)
git config --global credential.helper store

全局切换协议(对所有仓库生效)

# 以后 clone 自动用 SSH 替代 HTTPS
git config --global url."git@github.com:".insteadOf "https://github.com/"

四、场景二:GitHub 慢 → 镜像站

国内常用的 Git 镜像站:

镜像站地址特点
GitCode(CSDN)https://gitcode.com/国内速度快,有仓库镜像功能
Gitee(码云)https://gitee.com/最常用,支持从 GitHub 导入
清华 TUNAhttps://mirrors.tuna.tsinghua.edu.cn/github/大学镜像
gitclone.comhttps://gitclone.com/缓存代理型

方式 1:从镜像 clone,再改回 origin

# 先高速 clone
git clone https://gitclone.com/github.com/用户名/仓库名.git

cd 仓库名

# 改回 GitHub 真正的地址
git remote set-url origin https://github.com/用户名/仓库名.git

方式 2:全局用镜像代理(最推荐)

# 让所有 GitHub 请求自动走 gitclone 代理
git config --global url."https://gitclone.com/".insteadOf "https://github.com/"

# 恢复(去掉代理)
git config --global --unset url."https://gitclone.com/".insteadOf

五、场景三:双远程同步推送

一套代码同时推送到 GitHub + Gitee,双保险!

# GitHub 保持 origin
git remote -v
# origin  https://github.com/user/repo.git

# 添加 Gitee 作为第二个源
git remote add gitee https://gitee.com/user/repo.git

# 两条命令分别推送
git push origin main
git push gitee main

# 或者:让 origin 同时推两个地址
git remote set-url origin --add https://gitee.com/user/repo.git

# 现在一次 git push origin 就推两个地方了!
git push origin main

如果想撤回多地址配置:

# 先看看当前 origin 配了几个地址
git remote -v

# 删掉所有地址
git remote remove origin

# 重新添加
git remote add origin https://github.com/user/repo.git

六、场景四:SSH Config 加速(国内必备)

编辑 ~/.ssh/config,加上这段:

Host github.com
    HostName ssh.github.com
    Port 443
    User git
    IdentityFile ~/.ssh/id_ed25519

Host gitee.com
    HostName gitee.com
    User git
    IdentityFile ~/.ssh/id_ed25519

效果:GitHub 走 443 端口的 SSH,国内网络环境下稳定性大幅提升!

七、场景五:HTTP/HTTPS 代理配置

公司内网或科学上网场景。

为 Git 设置代理

# HTTP 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# SOCKS5 代理(Clash/Shadowsocks 等)
git config --global http.proxy socks5://127.0.0.1:7891
git config --global https.proxy socks5://127.0.0.1:7891

只给 GitHub 设代理

git config --global http.https://github.com.proxy socks5://127.0.0.1:7891

查看/取消代理

# 查看
git config --global --get http.proxy

# 取消
git config --global --unset http.proxy
git config --global --unset https.proxy

八、场景六:submodule 批量换源

# 配置 submodule 也走镜像
git config --global url."https://gitclone.com/".insteadOf "https://github.com/"

# 更新 submodule 时自动走代理
git submodule update --init --recursive

手动修改 .gitmodules 文件也能批量改 submodule 的 URL。

九、场景七:一键换源脚本

把下面内容保存为 git-switch-source.sh:

#!/bin/bash
# Git 一键换源脚本
# 用法: ./git-switch-source.sh [github|gitee|mirror|direct]

set -e

case "$1" in
  github)
    echo "切换到 GitHub 直连..."
    git remote set-url origin $(git remote get-url origin | sed 's|https://gitee.com/|https://github.com/|')
    git config --global --unset url."https://gitclone.com/".insteadOf 2>/dev/null || true
    ;;
  gitee)
    echo "切换到 Gitee..."
    git remote set-url origin $(git remote get-url origin | sed 's|https://github.com/|https://gitee.com/|')
    ;;
  mirror)
    echo "启用镜像加速..."
    git config --global url."https://gitclone.com/".insteadOf "https://github.com/"
    ;;
  direct)
    echo "关闭镜像加速..."
    git config --global --unset url."https://gitclone.com/".insteadOf
    ;;
  *)
    echo "用法: $0 [github|gitee|mirror|direct]"
    exit 1
    ;;
esac

echo "✅ 切换完成!当前远程源:"
git remote -v

十、常见问题速查

Q1: git push 报 403 / Permission denied

# HTTPS:检查是否开了 credential helper
git config --global credential.helper store

# SSH:检查 key 是否添加
ssh -T git@github.com

Q2: clone 到一半失败

# 浅克隆(只要最新版本,速度快)
git clone --depth 1 https://github.com/user/repo.git

# 断点续传
git fetch --all

Q3: proxy 配了但不管用

# 确认代理端口正确
curl -x socks5://127.0.0.1:7891 https://github.com

# 如果 curl 能通但 git 不通,可能是 git 没用到 proxy
git config --global --list | grep proxy

Q4: 想知道当前到底走的哪个源

git remote -v
git config --global --list | grep -E "(url|proxy|insteadOf)"

小结: 在国内做开发,最快的 Git 组合拳是 SSH 协议 + 443 端口 + 镜像站 fallback + 备用 Gitee 远程。一套配好,告别 git clone 焦虑 👍