Git proxy

starlitxiling Lv3

https://missing-semester-cn.github.io/2020/version-control/


https://jiapeng.me/git-ssh-proxy/

git设置代理

1.使用环境变量

1
2
3
4
5
6
export http_proxy=http://proxyuser:proxypwd@proxy.server.com:8080
export https_proxy=http://proxyuser:proxypwd@proxy.server.com:8080

eg:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

如果要取消,关闭当前终端再开一个就好了,上面只是一个临时设置,当然也可以运行下面的命令:

1
2
unset http_proxy
unset https_proxy

2.git全局配置

1
2
3
4
5
6
7
8
9
10
11
12
13
git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config --global https.proxy https://proxyuser:proxypwd@proxy.server.com:8080

eg:
git config --global http.proxy http://127.0.0.1:7890

git config --global https.proxy http://127.0.0.1:7890
~~~~
如果要取消的话,则运行一下命令:
~~~bash
git config --global --unset http.proxy
git config --global --unset https.proxy

3. Git 仓库特定配置

如果只是想为特定的 Git 仓库设置代理,可以在该仓库目录下运行以下命令:

1
2
3
4
5
6
7
8
git config http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config https.proxy https://proxyuser:proxypwd@proxy.server.com:8080


eg:
git config http.proxy http://127.0.0.1:7890
git config https.proxy http://127.0.0.1:7890

取消的话同全局配置一样就行

4. 使用 SSH ProxyCommand

在 SSH 的配置文件(通常是 ~/.ssh/config)中,您可以使用 ProxyCommand 来指定代理。这对于使用 SSH 方式连接的 Git 仓库非常有用。

1
2
3
Host github.com
User git
ProxyCommand nc -x proxy.server.com:8080 %h %p

取消的话在相关文件中删除这段配置就好了。

5. Git 配置文件

.gitconfig 文件中(位于您的用户目录下),您可以手动编辑并添加代理配置。

1
2
3
4
5
[http]
proxy = http://proxyuser:proxypwd@proxy.server.com:8080
[https]
proxy = https://proxyuser:proxypwd@proxy.server.com:8080

取消的话在相关文件中删除这段配置就好了。

6. 使用 Git 的core.sshCommand

可以通过设置 core.sshCommand 来定义 Git 应该使用的 SSH 命令,包括其中的代理设置。

1
2
3
4
5
git config --global core.sshCommand 'ssh -o ProxyCommand="ssh -W %h:%p proxyuser@proxy.server.com"'

eg:
git config --global core.sshCommand 'ssh -o ProxyCommand="nc -x 127.0.0.1:7890 %h %p"'

如果要取消的话,则运行一下命令:

1
git config --global --unset core.sshCommand

7. 使用 netrc 文件

有时候,可能需要对 Git 仓库的认证进行代理。虽然这不是代理网络连接,但 ~/.netrc 文件可以帮助您管理访问仓库的凭据。

1
2
3
4
machine proxy.server.com
login proxyuser
password proxypwd

取消的话在相关文件中删除这段配置就好了。

git网络测试

1. Ping 远程仓库服务器

如果您知道远程 Git 服务器的域名,可以使用 ping 命令来测试网络连通性。

1
ping github.com

如果 ping 返回了响应时间,那么您的计算机能够到达远程服务器。

2. 使用 git ls-remote 命令

git ls-remote 命令允许您查询远程仓库的引用,如分支和标签。如果此命令成功,表明 Git 能够通过网络连接到远程仓库。

1
2
git ls-remote <remote-url>

3. 使用 SSH 进行测试

如果通常使用 SSH 来连接到 Git 服务器,可以尝试以下命令来测试 SSH 连接:

1
ssh -T git@github.com

对于 GitHub,这个命令应该返回一个消息,告诉已经成功认证,但 GitHub 不提供 shell 访问。

4.使用 curl 测试 HTTP/HTTPS 连接

可以使用 curl 命令来测试 HTTP 或 HTTPS 连接:

1
curl -I https://github.com

这将返回 HTTP 请求的头部信息,包括响应状态码,例如 200 OK 表示连接成功。

5.检查代理设置

1
2
git config --global --get http.proxy
git config --global --get https.proxy

6.查看 Git 配置和日志

检查 Git 配置和日志也可以提供连接问题的线索:

1
2
3
git config --list
GIT_CURL_VERBOSE=1 git ls-remote <remote-url>

设置 GIT_CURL_VERBOSE=1 可以让 Git 在尝试网络操作时显示详细的信息。

如果是git push的时候出问题,可以使用

1
2
3
GIT_TRACE=1 git push
or
GIT_CURL_VERBOSE=1 git push

这两条命令来查看git push时产生的输出,通过输出定位出现的问题

  • Title: Git proxy
  • Author: starlitxiling
  • Created at : 2024-06-26 12:04:00
  • Updated at : 2024-09-14 21:58:41
  • Link: http://starlitxiling.github.io/2024/06/26/Git-proxy/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments