GIT is becoming very popular as SCM due to its simplicity and flexibity. Often software developers encounter proxy issue when the server sit behind firewall. There are three protocols git can use to clone a repository: http, git and ssh.

proxy setup](/uploads/git/git-proxy-setup.jpg)

GIT clone example:

  1. git clone http://git.denx.de/u-boot.git
  2. git clone git://git.denx.de/u-boot.git
  3. git clone ssh://git@github.com/weweng/blog.git

In this post, I explain how to set up proxy for each protocol using proxy.esl.cisco.com as example.

HTTP

There are two approaches: env variable and git config

set http_proxy enviroment variable

weng@weng-ucs1:$ export http_proxy='http://proxy.esl.cisco.com:8080'

or

git config

git config --global http.proxy http://proxy.esl.cisco.com:8080

weng@weng-ucs1:$ cat ~/.gitconfig 
[http]
	proxy = http://proxy.esl.cisco.com:8080

then it works as following:

git clone http://git.denx.de/u-boot.git

GIT

First install corkscrew using apt-get (since I’m running ubuntu) sudo apt-get install corkscrew

Second, create a bash script named as “git-proxy”

weng@weng-ucs1:$ cat /usr/local/bin/git-proxy 
#!/bin/sh

exec /usr/bin/corkscrew proxy.esl.cisco.com 8080 $* 
weng@weng-ucs1:$ git config  --global   core.gitProxy git-proxy
weng@weng-ucs1:$

Finally, it works as following:

git clone git://git.denx.de/u-boot.git

SSH

This is most complicated scenario. I use github.com as example.

Step#1 create an entry in ~/.ssh/config like below:

weng@weng-ucs1:$ cat /home/weng/.ssh/config
Host github.com
  Hostname github.com
  User git
  ProxyCommand nc -x proxy.esl.cisco.com:1080 %h %p
weng@weng-ucs1:$ 

Step#2 Generate RSA key and add into github.com Please take the steps from https://help.github.com/articles/generating-ssh-keys/

weng@weng-ucs1:$ ssh-keygen -t rsa -C "weweng@gmail.com"
...

weng@weng-ucs1:$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChKci3nRcHGZ5v1OspCiIX8vj72VCCksuILKjaFC5Q+u95AtWa1+kDhQj02ijA+RQGttWfEK5es7M9pfeeBHjtsuTSbbcReyKWi4qBIMKrGV6fyadeVad1Sj7GpV3eUUFZjjMTVgSZJ5lUK1JRpYjr2LuDD8etQgo7k0rwy3i/gIDlp2U4marG/0oPB2btLI/CgUcbBnNX/hsuR1P/hRX31HUp+Ru5UxypHNSuyh6YH6e0i2vfvbKcnU0xnk1ed7dBD86Np8uu76jfM5MdNe1AvPUx4+oJM0c6yf6e55sco5wLy8nTDgRftVW4wbbe93Y+zxwXU/AsW2HNakh0mR5L weweng@gmail.com
weng@weng-ucs1:$ 

Add above key into your guthub.com account, after that, then we should see following:

weng@weng-ucs1:$ ssh github.com
PTY allocation request failed on channel 0
Hi weweng! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
weng@weng-ucs1:$ 

Now we can veirfy git over ssh work:

weng@weng-ucs1:$ git clone ssh://git@github.com/weweng/blog.git
Cloning into 'blog'...
remote: Counting objects: 157, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 157 (delta 6), reused 0 (delta 0), pack-reused 142
Receiving objects: 100% (157/157), 2.30 MiB | 1.17 MiB/s, done.
Resolving deltas: 100% (57/57), done.
weng@weng-ucs1:$