﻿---
title: Set Git Proxy
date: 2025-05-30
excerpt: Set up a proxy for Git operations, both for HTTP/HTTPS and SSH connections.
tags:
  - OS
  - Git
  - SSH
cover: https://assets.vluv.space/cover/ToolChain/git_proxy.webp
updated: 2026-05-08 22:10:51
---

## Git Proxy

### HTTP & HTTPS

当使用的代理软件在7890端口提供 HTTP 代理服务时，可以在 `~/.gitconfig` 中添加如下配置，让 Git 的 HTTP 和 HTTPS 请求走代理

```gitconfig
[https]
    proxy = http://127.0.0.1:7890
[http]
    proxy = http://127.0.0.1:7890
```

如果只想让 github 走代理，可以在 `~/.gitconfig` 里添加如下配置

```gitconfig
[http "https://github.com"]
    proxy = http://127.0.0.1:7890
[https "https://github.com"]
    proxy = http://127.0.0.1:7890
```

### SSH

修改 `~/.ssh/config`，添加 `ProxyCommand` 配置项走代理即可。

> [!WARN] Proxy Port
>
> SSH 代理通常需要使用  **SOCKS5 端口**，无法通过普通 HTTP 代理端口转发。
>
> 因个人在 Clash Verge Rev 中启用了混合端口（Mixed Port），这里在 ProxyCommand 使用的仍为 `127.0.0.1:7890`，它同时支持 HTTP 和 SOCKS5 协议。相关介绍参考[代理端口 - 虚空终端 Docs](https://wiki.metacubex.one/config/inbound/port/)

#### Windows Example

Windows下载的Git自带了connect. exe，可以用它来做代理转发。个人使用scoop安装的Git，`connect.exe` 路径为 `D:\envir_vars\scoop\apps\git\current\mingw64\bin\connect.exe`，请根据实际路径修改

```ssh-config
Host github.com
  User git
  Hostname ssh.github.com
  Port 443
  IdentityFile "~/.ssh/id_rsa"
  TCPKeepAlive yes
  ProxyCommand "D:\envir_vars\scoop\apps\git\current\mingw64\bin\connect.exe" -S 127.0.0.1:7890 %h %p // [!code ++]
```

#### Mac Example

Mac可以用系统自带的 `nc` 来做代理转发

```ssh-config
Host github.com
  User git
  Hostname ssh.github.com
  Port 443
  IdentityFile "~/.ssh/id_rsa"
  TCPKeepAlive yes
  ProxyCommand nc -X 5 -x 127.0.0.1:7890 %h %p // [!code ++]
```
