Git with and without proxy

I use Git both at home and at work; at home I’ve got a direct connection but at work I must connect through a SOCKS proxy for external access.

Although Git supports proxy configuration, it’s irritating to have to change this all the time depending on my location. Setting the environment variable GIT_PROXY_COMMAND overrides whatever proxy configuration already exists - this is all we need to get git:// repositories to work. To get Git-over-SSH connections to work, which you probably want for push operations, you’ll also need to set up GIT_SSH.

I’ve created some simple shell scripts to help. The first is a simple wrapper for connect.c (which you may need to compile, of course).

#!/bin/sh
# Filename: ~/bin/socks-gw
# This script connects to a SOCKS proxy using connect.c
/path/to/connect -S my.socks.server:1080 $@

The second is another simple wrapper for SSH through a SOCKS proxy:

#!/bin/sh
# Filename: ~/bin/socks-ssh
# This script opens an SSH connection through a SOCKS server
ssh -o ProxyCommand="${HOME}/bin/socks-gw %h %p" $@

Lastly, I have a script I source when at work to set the environment variables for me:

#!/bin/sh
# Filename: ~/bin/work
# This script sets Git to use the SOCKS proxy
export GIT_SSH="${HOME}/bin/socks-ssh"
export GIT_PROXY_COMMAND="${HOME}/bin/socks-gw"

And I’ve put an alias in my .bash_profile to help me remember to source it instead of running it:

alias work="source ${HOME}/bin/work"

Now when I use Git at home, it just works without having to remove any proxy config, and when I use it at work I just type work in my shell and it works there too…

14 Responses to “Git with and without proxy”

  1. Jonathan Quimbly says:

    “You have died of linkrot.”

    Okay, not really, but your link to connect.c is dead.

  2. admin says:

    Hmm, looks as though there’s some kind of configuration error at the other end. Links updated to point to the file directly in the meantime.

    Thanks!

  3. james says:

    thank you for this. you are a champion.

  4. Proxy says:

    Just wanna add something here. Need a fast and clean proxy? Try proxy.my

    Unblock friendster, facebook, myspace and even youtube!

  5. Ludovic Kuty says:

    Awesome ! Many thanks.

  6. Dan says:

    Awesome… Some git repos will access fine through http so I can use my work http proxy, but this is great for the ones that don’t respond nicely to http paths.

  7. Border says:

    This is great. TKS

  8. [...] to ThreeBytesFull, from which I re-adapted the scripts to work on [...]

  9. androidstar says:

    whenever i execute
    repo init -u git://android.git.kernel.org/platform/manifest.git
    i get following error please help me

    Getting repo …
    from git://android.git.kernel.org/tools/repo.git
    FATAL: failed to begin relaying via HTTP.
    fatal: The remote end hung up unexpectedly

  10. admin says:

    Ensure that your GIT_SSH and GIT_PROXY_COMMAND environment variables are set as described above. The repository URL you’re using isn’t HTTP so I’m not sure where the HTTP errors are coming from. Is that the current version of the repo script from http://source.android.com/download/using-repo?

  11. androidstar says:

    http error is from the connect script

  12. androidstar says:

    whenever i execute
    repo init -u git://android.git.kernel.org/platform/manifest.git

    i get error

    FATAL: Connection closed by peer.
    fatal: The remote end hung up unexpectedly

    if i use socks-gw as above

    if i change socks-gw to

    /path/to/connect -H my.socks.server:1080 $@

    i get following error

    Getting repo …
    from git://android.git.kernel.org/tools/repo.git
    FATAL: failed to begin relaying via HTTP.
    fatal: The remote end hung up unexpectedly

    Thanks
    suma

  13. Jorge says:

    Hi,

    If I only have a http proxy, for instance, 172.22.22.22:8080 How can I use your script to download a git repository? I need the connec -S or -H parameter? I’m trying to clone a repository without http access: git clone git://gitorious.org/vjaquez-beagleboard/marmita.git

Leave a Reply