Please enable Javascript to view the contents

科学上网(2) GIT配置proxy并忽略内网仓库

 ·  ☕ 2 分钟

引言

由于GitHub访问需要通过科学上网,因此需要配置git客户端通过proxy与github通信;但公司内网的git仓库不能走proxy,否则访问不通。因此需要查找git配置实现如下状态:

  • 所有github仓库走代理
  • 所有内网仓库不走代理

现状

由于github仓库拉取缓慢,所以配置了代理; 但内网仓库拉取也走了代理,导致出现以下报错

1
2
hex@hex-pc:~/example-repo$ git pull 
fatal: unable to access 'https://git.service.rd/plugins/git/example-repo.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.

办法一(推荐)

git是可以根据环境变量的配置,声明代理并将内网地址配置不走代理,实现此功能,但要注意格式。

1
2
3
4
5
6
7
export https_proxy=http://127.0.0.1:10800/
export http_proxy=http://127.0.0.1:10800/
export all_proxy=socks://127.0.0.1:1088/
export ftp_proxy=http://127.0.0.1:10800

# 声明内网不走proxy的服务 声明域名作为通配,以空格分隔。
export no_proxy=".service.rd .local.domain 127.0.0.1 localhost"

方法二:修改本地库-local配置

也可以针对仓库设置git config。可以设置本地库全局配置,也可以只设置单一远端。

本地仓库全局设置

进入仓库目录下,为当前仓库,添加代理(仓库级别)。

1
2
git config --local --add http.proxy "http://127.0.0.1:10808"
git config --local --add https.proxy "http://127.0.0.1:10808"

在仓库目录下<repo_path>/.git/config,会出现以下内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/hex-go/example.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[http]
	proxy = http://127.0.0.1:10808
[https]
	proxy = http://127.0.0.1:10808

删除设置

1
2
git config --local --unset http.proxy
git config --local --unset https.proxy

本地仓库特定远端

进入仓库目录下,为名称为<name>的远端,添加代理。

1
git config --local --add remote.<name>.proxy "http://127.0.0.1:10808"

执行下面命令查看

1
git config --local --get remote.<name>.proxy

方法三:修改全局配置(只能针对单库)

修改 ~/.gitconfig文件,增加以下内容

1
2
3
4
5
[http]
   sslVerify = true
[http "https://git.service.rd/plugins/git/example-repo.git"]
   sslVerify = false
   ptoxy = http://127.0.0.1:10808
分享

Hex
作者
Hex
CloudNative Developer

目录