supereagle / experiences

Summary of practical experience in work.
2 stars 0 forks source link

Change the default git protocol for `go get` from HTTPS to SSH #31

Closed supereagle closed 7 years ago

supereagle commented 7 years ago

公司内部可以ping通外部网站,但是浏览器上网必须使用代理才能。因此,这个代理只是针对http,https等部分协议做了限制,而对其他协议是没有限制的,例如:ssh。

$ ping github.com

正在 Ping github.com [192.30.255.112] 具有 32 字节的数据:
来自 192.30.255.112 的回复: 字节=32 时间=201ms TTL=49
来自 192.30.255.112 的回复: 字节=32 时间=200ms TTL=49
来自 192.30.255.112 的回复: 字节=32 时间=198ms TTL=49
来自 192.30.255.112 的回复: 字节=32 时间=197ms TTL=49

192.30.255.112 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 197ms,最长 = 201ms,平均 = 199ms

# go get默认使用的git protocol是HTTPS
$ go get github.com/Microsoft/go-winio
# cd .; git clone https://github.com/Microsoft/go-winio E:\gocode\src\github.com\Microsoft\go-winio
Cloning into 'E:\gocode\src\github.com\Microsoft\go-winio'...
fatal: unable to access 'https://github.com/Microsoft/go-winio/': Failed to connect to github.com port 443: Timed out
package github.com/Microsoft/go-winio: exit status 128

# 手动使用SSH的git protocol可以正常git clone
$ git clone git@github.com:coreos/clair.git /e/gocode/src/github.com/coreos/clair
Cloning into 'E:/gocode/src/github.com/coreos/clair'...
remote: Counting objects: 6647, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 6647 (delta 1), reused 2 (delta 1), pack-reused 6631
Receiving objects: 100% (6647/6647), 14.08 MiB | 377.00 KiB/s, done.
Resolving deltas: 100% (2725/2725), done.
supereagle commented 7 years ago

Solutions

针对单个git项目

对于已经git clone下来的项目,如果其还是使用HTTPS协议的话,是无法进行git pull等操作的,同样提示port 443: Timed out。可以将该项目的git协议由HTTPS修改为SSH。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = git@github.com:supereagle/kubernetes # Changed from `https://github.com/kubernetes/kubernetes.git`
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[user]
        name = supereagle
        email = jmyue@hotmail.com

修改git默认协议配置

手动修改项目的git config,需要在项目已经git clone下来之后才能进行。对于少量的项目,这种更改每个项目git config的方法还可以凑效。但是,对于大量项目的情况,这种方法就不可行了。尤其是Go的项目依赖非常多,而且都是通过go get命令批量下载依赖。因此,必须采用修改git global config的方法,针对所有项目都生效。

在当前用户目录下新建文件.gitconfig,并添加如下内容:

$ cat /c/Users/robin/.gitconfig
[url "git@github.com:"]
        insteadOf = https://github.com

或者直接通过git config --global命令配置:

$ git config --global url."git@github.com:".insteadOf "https://github.com"

检查配置是否生效:

$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=F:/Git/mingw32/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.required=true
filter.lfs.process=git-lfs filter-process
pack.packsizelimit=2g
url.git@github.com:.insteadof=https://github.com  # The added line

References