git命令使用备忘
3

<think>

在Git中配置代理可以通过设置`http.proxy`和`https.proxy`来实现。如果你希望所有的`git clone`和`git pull`操作都通过代理进行,可以全局设置代理。以下是具体步骤:

### 1. 设置全局代理
你可以使用以下命令设置全局的HTTP和HTTPS代理:

```bash
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy https://proxy.example.com:8080
```

将`proxy.example.com:8080`替换为你的代理服务器地址和端口。

如果你的代理服务器需要用户名和密码,可以这样设置:

```bash
git config --global http.proxy http://username:[email protected]:8080
git config --global https.proxy https://username:[email protected]:8080
```

注意:在URL中包含密码可能存在安全风险,因为其他用户可能看到你的配置(通过`git config --list`)。考虑使用环境变量或安全的凭据存储方式。

### 2. 使用socks5代理
如果你使用SOCKS5代理(比如Shadowsocks),可以这样设置:

```bash
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
```

端口`1080`是SOCKS代理的默认端口,根据你的实际情况替换。

### 3. 仅对特定网站使用代理
如果你只想对特定的网站使用代理,比如GitHub,可以这样设置:

```bash
git config --global http.https://github.com.proxy http://proxy.example.com:8080
git config --global https.https://github.com.proxy https://proxy.example.com:8080
```

### 4. 取消代理设置
如果想取消代理,可以使用以下命令:

```bash
git config --global --unset http.proxy
git config --global --unset https.proxy
```

### 5. 查看当前配置
可以使用以下命令查看当前的代理配置:

```bash
git config --global --get http.proxy
git config --global --get https.proxy
```

### 6. 使用环境变量
除了在Git配置中设置,你还可以通过环境变量设置代理。在命令行中执行:

```bash
export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"
```

或者对于socks5代理:

```bash
export http_proxy="socks5://127.0.0.1:1080"
export https_proxy="socks5://127.0.0.1:1080"
```

这种设置方式只对当前终端会话有效。如果想永久生效,可以将这些行添加到你的shell配置文件(如`~/.bashrc`或`~/.zshrc`)中。

### 注意事项:
- 代理服务器必须支持HTTP和HTTPS代理。
- 如果你的代理设置导致问题,记得可以用`git config --global --unset`来取消设置。
- 在某些网络环境下,可能还需要设置`no_proxy`环境变量来排除一些不需要代理的地址。

根据你的实际代理服务器情况,选择上述方法之一进行配置即可。

</think>


要在 `git clone` 或 `git pull` 时默认使用代理,可通过以下方法配置 Git 的全局代理设置:

---

### 方法 1:命令行永久配置(推荐)
运行以下命令设置全局 HTTP/HTTPS 代理(替换 `127.0.0.1:1080` 为你的代理地址):
```bash
# 设置 HTTP 代理
git config --global http.proxy http://127.0.0.1:1080

# 设置 HTTPS 代理
git config --global https.proxy http://127.0.0.1:1080
```
- **适用场景**:所有 Git 操作自动走代理。
- **取消代理**:  
  ```bash
  git config --global --unset http.proxy
  git config --global --unset https.proxy
  ```

---

### 方法 2:命令行临时代理(单次生效)
在命令前添加环境变量(替换端口):
```bash
# HTTP 代理
http_proxy=http://127.0.0.1:1080 git clone https://github.com/xxx/xxx.git

# HTTPS 代理
https_proxy=http://127.0.0.1:1080 git pull
```

---

### 方法 3:配置 SOCKS5 代理(如 Shadowsocks)
```bash
# 设置 SOCKS5 代理
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
```

---

### 方法 4:仅针对特定域名使用代理(如 GitHub)
```bash
# 只对 github.com 走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
```

---

### 验证代理是否生效
```bash
# 查看全局配置
git config --global --get http.proxy
git config --global --get https.proxy
```

---

### 常见问题排查
1. **代理地址错误**  
   确保端口正确(如 `1080`、`7890` 等),协议类型匹配(HTTP/HTTPS/SOCKS5)。
2. **代理工具未运行**  
   检查 Clash/Shadowsocks 等代理工具是否开启并监听对应端口。
3. **环境变量冲突**  
   如果同时存在系统环境变量(如 `ALL_PROXY`)和 Git 配置,代理可能重复生效导致失败。

---

### 示例:Clash 代理配置(HTTP 端口为 7890)
```bash
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
```

> ✅ 配置后,所有 `git clone`/`git pull` 操作默认通过代理进行。

#git clone pull使用代理

git命令使用备忘
http://localhost:8090/archives/gitzhi-shi
作者
Administrator
发布于
更新于
许可